如果您想获取单个帖子的 Wordpress 和 Discourse 评论总数,那么 get_comments_number() 将会起作用(即它将返回两者的总和)。
如果您想获取您网站上所有评论的总数(而不是来自特定帖子的评论),无论是来自 Wordpress 还是 Discourse,这是一个有些主观的问题。这取决于您对“所有评论”的定义。您需要为此编写一个自定义函数,如果您只是单独计算 Wordpress 评论,您也会这样做。您可以这样做,并在简码或块中使用它
function count_all_comments() {
$wordpress_count = wp_count_comments();
$count = $wordpress_count->approved;
$query_args = array(
'post_status' => 'publish',
);
$query = new WP_Query($query_args);
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
$count += intval( get_post_meta( get_the_ID(), 'discourse_comments_count', true ) );
}
}
return $count;
}
(我只是即时编写了这段代码,请在使用前进行测试
)
请注意,我选择返回已批准的 Wordpress 评论数以及已发布帖子的 Discourse 评论数。您可以根据您的计数标准做出其他选择。