有的网站管理员会为网站添加评论功能,默认状态下文档评论开头是开启的;我们在WordPress管理后台文章发表/修改右侧工具条会看到讨论模块,其中有一项[允许评论],如果打开该开关,并且有配置评论功能,系统将允许用户留言。
在前端判断是否开启评论的WordPress标签:
<?php comments_open(); ?>
实际应用中,根据实际测试及使用,当文档后台开启允许评论,会返回1,否则会返回假,例如:
<?php
$comments_open = comments_open();
//$comments_open == 1
if($comments_open)
{
echo 1;
}
?>
WordPress允许管理员开启或关闭评论
comments_open()源代码:
comments_open() 位于 /wp-includes/comment-template.php 行1239[5.9.3版本]
/**
* Determines whether the current post is open for comments.
*
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
*
* @since 1.5.0
*
* @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
* @return bool True if the comments are open.
*/
function comments_open( $post_id = null ) {
$_post = get_post( $post_id );
$post_id = $_post ? $_post->ID : 0;
$open = ( $_post && ( 'open' === $_post->comment_status ) );
/**
* Filters whether the current post is open for comments.
*
* @since 2.5.0
*
* @param bool $open Whether the current post is open for comments.
* @param int $post_id The post ID.
*/
return apply_filters( 'comments_open', $open, $post_id );
}
实际使用中使用方法:在模板中直接判断:
<?php
if(comments_open()){
echo "WordPress评论开启";
}
?>