前些天,倡萌和恒創科技聯合舉辦了一次踢樓活動(詳見),活動結束后要統計此次參與踢樓的用戶數量,也就是要統計提交了評論的用戶數,在網上搜索了一下,發現 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); ?>
新聞熱點
疑難解答