检索帖子的评论数量。
原型
get_comments_number( int|WP_Post $post_id )
参数
$post_id
(int|WP_Post)
(Optional)
发布ID或WP_Post对象。默认是全球$ post。
返回值
(string|int)
如果帖子存在,则表示帖子的评论数量的数字字符串,否则为0。
源文件
路径:wp-includes/comment-template.php
<?php
...
function get_comments_number( $post_id = 0 ) {
$post = get_post( $post_id );
if ( ! $post ) {
$count = 0;
} else {
$count = $post->comment_count;
$post_id = $post->ID;
}
/**
* Filters the returned comment count for a post.
*
* @since 1.5.0
*
* @param string|int $count A string representing the number of comments a post has, otherwise 0.
* @param int $post_id Post ID.
*/
return apply_filters( 'get_comments_number', $count, $post_id );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/get_comments_number/