方法一.直接调取文章评论总数:
<?php $id=$post->ID; echo get_post($id)->comment_count;?><!--直接调取文章评论总数-->
方法二.WordPress 自带函数可调用文章评论总数
某些特殊情况需要统计某文章实际评论的人数,非评论数,我们可以根据评论者的邮箱进行筛选统计。方法如下:
直接将下面的函数添加到当前主题的 functions.php 中。
<?php
function zfunc_comments_users($postid=0,$which=0) {
$comments = get_comments('status=approve&type=comment&post_id='.$postid); //获取文章的所有评论
if ($comments) {
$i=0; $j=0; $commentusers=array();
foreach ($comments as $comment) {
++$i;
if ($i==1) { $commentusers[] = $comment->comment_author_email; ++$j; }
if ( !in_array($comment->comment_author_email, $commentusers) ) {
$commentusers[] = $comment->comment_author_email;
++$j;
}
}
$output = array($j,$i);
$which = ($which == 0) ? 0 : 1;
return $output[$which]; //返回评论人数
}
return 0; //没有评论返回0
}
?>
调用方法:
<?php $postid=$post->ID; echo zfunc_comments_users($postid); ?>
参数说明:$postid 是需要获取评论人数的文章 ID
一般用法:在一般主题的loop里面可以这样用:
<?php echo zfunc_comments_users($post->ID); ?>
还可以输出评论总数,用法:
<?php $postid=$post->ID; echo zfunc_comments_users($postid, 1); ?>

