将以下代码添加到当前主题的 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
一般用法:在一般主题的 loop 里面可以这样用:
- <?php echo zfunc_comments_users($post->ID); ?>
输出评论总数的用法:
- <?php echo zfunc_comments_users($postid, 1); ?>
以上内容整理自@ZWWoOoOo