Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

WordPress获取文章的评论人数

WordPress 获取文章的评论人数!前些天,杜澔沐和亿亿科技联合举办了一次踢楼活动(详见),活动结束后要统计此次参与踢楼的用户数量,也就是要统计提交了评论的用户数。

WordPress 获取文章的评论人数!前些天,杜澔沐和亿亿科技联合举办了一次踢楼活动(详见),活动结束后要统计此次参与踢楼的用户数量,也就是要统计提交了评论的用户数。

在网上搜索了一下,发现杜澔沐已经折腾过这个功能,试了一下,还可以用,分享过来,有需要的朋友不妨试试。

直接将下面的函数添加到当前主题的 functions.php

/* 获取文章的评论人数 by 杜澔沐 | dhaomu.com */
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); ?>
温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
编程技巧

织梦教程列表页如何调用栏目关键词和描述

2023-9-5 0:00:00

编程技巧

宝塔开启如何开启OPcache

2023-10-5 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索