万が一、当サイトで重大な問題を発見した際などは、フォーラムや WordSlack #docs チャンネルでお知らせください。</p>
関数リファレンス/comments open
この条件分岐タグは、現在の(または指定された)投稿に対するコメントが許可されているかどうかをチェックします。これは真偽値、つまり TRUE または FALSE を返す関数です。
使い方
<?php comments_open( $post_id ); ?>
パラメータ
- $post_id
- (整数|オブジェクト) (オプション) 投稿の ID、または投稿オブジェクト(
WP_Post
)。- 初期値:
null
(現在の投稿)
- 初期値:
戻り値
- (真偽値)
- コメントが許可されていれば true、それ以外は false。
用例
次のコードを使うと、固定ページでのコメントを常に禁止できます。
注意:テーマが comments_open()
を用いてコメントの可否をチェックしている必要があります。
add_filter( 'comments_open', 'my_comments_open', 10, 2 ); function my_comments_open( $open, $post_id ) { $post = get_post( $post_id ); if ( 'page' == $post->post_type ) $open = false; return $open; }
次のコードを使うと、カスタムフィールド "Allow Comments"
に 1
がセットされている投稿に対するコメントを許可できます。これは 設定 » ディスカッション設定 » X 日以上前の投稿のコメントフォームを自動的に閉じる が有効でありながら、幾つかの古い投稿のみコメントを許可したいときに便利です。
add_filter( 'comments_open', 'my_comments_open', 10, 2 ); function my_comments_open( $open, $post_id ) { $post = get_post( $post_id ); if ( get_post_meta( $post->ID, 'Allow Comments', true ) ) $open = true; return $open; }
参考
- チェック結果を返す前に apply_filters() を使って
'comments_open'
フィルターフックを呼び出します。パラメータは、チェック結果と$post_id
です。
変更履歴
1.5.0 : 新規導入。
ソースファイル
comments_open() は wp-includes/comment-template.php
にあります。
関連資料
条件分岐タグ: is_404(), is_admin(), is_admin_bar_showing(), is_archive(), is_attachment(), is_author(), is_category(), is_comments_popup(), is_date(), is_day(), is_feed(), is_front_page(), is_home(), is_local_attachment(), is_main_query, /is_multi_author, is_month(), is_new_day(), is_page(), is_page_template(), is_paged(), is_plugin_active(), is_plugin_active_for_network() /en, is_plugin_inactive() /en, is_plugin_page() /en, is_post_type_archive(), is_preview() /en, is_search(), is_single(), is_singular(), is_sticky(), is_tag(), is_tax(), is_taxonomy_hierarchical(), is_time(), is_trackback(), is_year(), in_category(), in_the_loop(), is_active_sidebar(), is_active_widget(), is_blog_installed() /en, is_rtl(), is_dynamic_sidebar(), is_user_logged_in(), has_excerpt(), has_post_thumbnail(), has_tag(), pings_open(), email_exists(), post_type_exists(), taxonomy_exists(), term_exists(), username_exists() /en, wp_attachment_is_image(), wp_script_is()
コメント関数: cancel_comment_reply_link(), comment_author(), comment_author_email(), comment_author_email_link(), comment_author_IP(), comment_author_link(), comment_author_rss(), comment_author_url(), comment_author_url_link(), comment_class(), comment_date(), comment_excerpt(), comment_form_title(), comment_form(), comment_ID(), comment_id_fields() / en, comment_reply_link(), comment_text(), comment_text_rss(), comment_time(), comment_type(), comments_link, comments_number(), comments_open(), comments_popup_link(), comments_popup_script(), comments_rss_link(), get_avatar(), next_comments_link(), paginate_comments_links(), permalink_comments_rss(), previous_comments_link(), wp_list_comments()
最新英語版: WordPress Codex » Function Reference/comments_open (最新版との差分)