WordPress如何获取文章评论人数?小编为大家分享一下zwwooooo 大叔折腾的,有需要的朋友不妨试试。

直接将下面的函数添加到当前WordPress主题的 functions.php
/* 获取文章的评论人数 byJ H t T K zwwooooo | zww.me */
function zfunc_comments_users($postid=0,$which=0) {
$comments = get_comments(‘status=approve&type=comment&post_id=’.$postid); //获取h \ d X 3 y K文章的所有评论
if ($comments) {
$i=0; $j=0; $commentusers=array();
foreach ($comments as $commem u v 4 6 *nt) {
++$i;
if ($i==1) { $commentusers[] = $commen] y + m { u @t->comment_author_email; ++$j; }
if ( !in_array($comment->comment_author_email, $commentusers) ) {
$commV 9 7 n Wentusers[] = $comment->comment_author_email;
++$j;
}
}
$output = array($jq i ! 5 m,$i);
$which = ($which == 0) ? 0 : 1;
return $output[$which]; //返回评论人数
}
return 0; //没有评论返回0
}
调用方法:
<?php echo zfunc_comments_uT : H 8sers($postid); ?>
参数说明:$postid8 x b L 是需要获取评论人数的文章ID
一般用法1 I b \ – O X – _:在一般主题的loop里面可以这样用:
<?php eche y b 0 V A ^ , uo zfunct w 2 X + t :_comments_users($post->ID); ?>
PS:还可以输出评论总数,用法:
<?php echo zfunc_commentu n #s_users($pos$ q f O =tid, 1); ?>

