wp_count_comments(): Count Comments by Status in WordPress

Description

Gets the number of comments for the entire site or for a specific post.

Usage

<?php wp_count_comments( post_id ); ?>

Parameters

$post_id

(integer) (optional): The post ID for which you want to count comments.
Default: None

Return value

Returns the number of approved comments, pending comments, spam comments, deleted comments, and the total number of comments.

Examples

Default usage

Get the number of comments across the entire site.

<?php
$comments_count = wp_count_comments();
echo "整个网站中的评论:<br />";
echo "审核中: " . $comments_count->moderated . "<br />";
echo "已批准: " . $comments_count->approved . "<br />";
echo "垃圾评论: " . $comments_count->spam . "<br />";
echo "已删除评论: " . $comments_count->trash . "<br />";
echo "评论总数: " . $comments_count->total_comments . "<br />";
?>

Get the number of comments for a specific post

<?php
$comments_count = wp_count_comments( 2492 );
echo "文章 2492 <br />的评论数量:";
echo “审核中” . $comments_count->moderated . "<br />";
echo "已批准量: " . $comments_count->approved . "<br />";
echo "垃圾评论:" . $comments_count->spam . "<br />";
echo "已删除评论: " . $comments_count->trash . "<br />";
echo "评论总数: " . $comments_count->total_comments . "<br />";
?>

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *