comments_open()
云策文档标注
概述
comments_open() 函数用于判断当前文章是否允许评论,返回布尔值。它接受可选参数指定文章,并可通过 'comments_open' 过滤器进行自定义。
关键要点
- 函数返回布尔值:true 表示评论开放,false 表示关闭。
- 参数 $post 可选,可为文章 ID 或 WP_Post 对象,默认使用当前文章。
- 内部使用 get_post() 获取文章数据,检查 comment_status 是否为 'open'。
- 提供 'comments_open' 过滤器,允许开发者修改评论开放状态。
- 常用于主题开发中的条件判断,如结合 is_single() 控制脚本加载。
代码示例
// 示例1:仅在单篇文章且评论开放时加载脚本
function wpdocs_scripts(){
if ( is_single() && comments_open() ) {
wp_enqueue_script( 'wpdocs_script' );
}
}
add_action( 'wp_print_scripts', 'wpdocs_scripts' );
// 示例2:通过过滤器禁用页面评论
function wpdocs_comments_open( $open, $post_id ) {
$post = get_post( $post_id );
if ( 'page' == $post->post_type )
$open = false;
return $open;
}
add_filter( 'comments_open', 'wpdocs_comments_open', 10, 2 );
// 示例3:使用自定义字段控制评论开放
function wpdocs_comments_open( $open, $post_id ) {
$post = get_post( $post_id );
if (get_post_meta($post->ID, 'Allow Comments', true)) {
$open = true;
}
return $open;
}
add_filter( 'comments_open', 'wpdocs_comments_open', 10, 2 );注意事项
- 在 WordPress 4.3+ 中,页面评论默认禁用,但可通过过滤器覆盖。
- 使用过滤器时需注意优先级和参数数量,避免与其他插件冲突。
- 函数依赖于文章数据,确保在循环内或提供有效 $post 参数。
原文内容
Determines whether the current post is open for comments.
Description
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Parameters
Source
function comments_open( $post = null ) {
$_post = get_post( $post );
$post_id = $_post ? $_post->ID : 0;
$comments_open = ( $_post && ( 'open' === $_post->comment_status ) );
/**
* Filters whether the current post is open for comments.
*
* @since 2.5.0
*
* @param bool $comments_open Whether the current post is open for comments.
* @param int $post_id The post ID.
*/
return apply_filters( 'comments_open', $comments_open, $post_id );
}
Hooks
- apply_filters( ‘comments_open’, bool $comments_open, int $post_id )
-
Filters whether the current post is open for comments.
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 5 content
Codex
Enqueuing a script only if we’re seeing a single post and comments are open for the current post
/** * Enqueue wpdocs_script if viewing a post with comments enabled. */ function wpdocs_scripts(){ if ( is_single() && comments_open() ) { // wpdocs_script must have been previously registered via wp_register_script() wp_enqueue_script( 'wpdocs_script' ); } } add_action( 'wp_print_scripts', 'wpdocs_scripts' );Skip to note 6 content
Codex
With this code you can always disable comments on pages, assuming your theme uses comments_open() to check if the comments are open.
Note: Comments are disabled on pages by default in 4.3+.
/** * Disable comments on pages. * * @param bool $open Whether comments should be open. * @param int $post_id Post ID. * @return bool Whether comments should be open. */ function wpdocs_comments_open( $open, $post_id ) { $post = get_post( $post_id ); if ( 'page' == $post->post_type ) $open = false; return $open; } add_filter( 'comments_open', 'wpdocs_comments_open', 10, 2 );Skip to note 7 content
Evgeni
// Отключаем комментирование add_filter( 'comments_open', '__return_false' );Skip to note 8 content
Codex
With this code you can enable comments on a post that has custom field “Allow Comments” set to 1.
This is helpful when you have told WordPress to disable comments for posts that are older than X days but wish to enable comments for a handful of old posts.
/** * Enable or disable comments based on custom field Allow Comments. * * @param bool $open Whether comments should be open. * @param int $post_id Post ID. * @return bool Whether comments should be open. */ function wpdocs_comments_open( $open, $post_id ) { $post = get_post( $post_id ); if (get_post_meta($post->ID, 'Allow Comments', true)) { $open = true; } return $open; } add_filter( 'comments_open', 'wpdocs_comments_open', 10, 2 );