前些天,倡萌和恒創(chuàng)科技聯(lián)合舉辦了一次踢樓活動(dòng)(詳見),活動(dòng)結(jié)束后要統(tǒng)計(jì)此次參與踢樓的用戶數(shù)量,也就是要統(tǒng)計(jì)提交了評(píng)論的用戶數(shù),在網(wǎng)上搜索了一下,發(fā)現(xiàn) zwwooooo 大叔已經(jīng)折騰過這個(gè)功能,試了一下,還可以用,分享過來,有需要的朋友不妨試試。
直接將下面的函數(shù)添加到當(dāng)前主題的 functions.php
/* 獲取文章的評(píng)論人數(shù) by zwwooooo | zww.me */
function zfunc_comments_users($postid=0,$which=0) {
$comments = get_comments('status=approve&type=comment&post_id='.$postid); //獲取文章的所有評(píng)論
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]; //返回評(píng)論人數(shù)
}
return 0; //沒有評(píng)論返回0
}
調(diào)用方法:
<?php echo zfunc_comments_users($postid); ?>
參數(shù)說明:$postid 是需要獲取評(píng)論人數(shù)的文章ID
一般用法:在一般主題的loop里面可以這樣用:
<?php echo zfunc_comments_users($post->ID); ?>
PS:還可以輸出評(píng)論總數(shù),用法:
<?php echo zfunc_comments_users($postid, 1); ?>
新聞熱點(diǎn)
疑難解答
圖片精選