代码来自大叔ZWW,感谢分享。获取文章评论人数的需求实在少见……简直要用变态来形容。但是既然有需求就要记录下来~依旧是直接丢入functions.php 中不多说~
实现代码
/* 获取文章的评论人数 by zwwooooo | zww.me */ 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 echo zfunc_comments_users($postid); ?> //$postid为文章ID
如果在文章循环中使用,还可以:
<?php echo zfunc_comments_users($post->ID); ?>
或者是输出评论总数:
<?php echo zfunc_comments_users($postid, 1); ?>
学习了,blog.people8848.com
支持多说么?
好哒,等我明天试试