检索博客或单个帖子的总评论。
原型
wp_count_comments( int $post_id )
描述
返回对象的属性包含整个博客或单个帖子的“已审核”,“已批准”和垃圾评论。这些属性包含与状态匹配的注释量。 ‘total_comments’属性包含总评论的整数。
参数
$post_id
(int)
(Optional)
帖子ID。
返回值
(object|array)
评论统计。
源文件
路径:wp-includes/comment.php
<?php
...
function wp_count_comments( $post_id = 0 ) {
$post_id = (int) $post_id;
/**
* Filters the comments count for a given post.
*
* @since 2.7.0
*
* @param array $count An empty array.
* @param int $post_id The post ID.
*/
$filtered = apply_filters( 'wp_count_comments', array(), $post_id );
if ( ! empty( $filtered ) ) {
return $filtered;
}
$count = wp_cache_get( "comments-{$post_id}", 'counts' );
if ( false !== $count ) {
return $count;
}
$stats = get_comment_count( $post_id );
$stats['moderated'] = $stats['awaiting_moderation'];
unset( $stats['awaiting_moderation'] );
$stats_object = (object) $stats;
wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' );
return $stats_object;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_count_comments/