WordPress 获取文章的评论人数

前些天,倡萌和恒创科技联合举办了一次踢楼活动(详见),活动结束后要统计此次参与踢楼的用户数量,也就是要统计提交了评论的用户数,在网上搜索了一下,发现 zwwooooo 大叔已经折腾过这个功能,试了一下,还可以用,分享过来,有需要的朋友不妨试试。

直接将下面的函数添加到当前主题的 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); ?>

PS:还可以输出评论总数,用法:

<?php echo zfunc_comments_users($postid, 1); ?>

代码出自:http://zww.me/archives/25613

来源:

https://www.wpdaxue.com/wordpress-get-comment-users.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33107.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?