WordPress获取当前文章的参与评论者数量/网站全部评论数量

网站APP 18 天前 回复

,

开发主题的时候需要调用本篇文章中全部评论人员数量,这样直接把数量显示出来,显得很高大尚,参与互动人数很多,那么这个数量怎么调用?不多废话,上代码:

1.function里面添加

function 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
}

2.需要的地方添加调用代码

<?php echo comments_users($post->ID); ?>

当然也可以用来调用全站的总评论数:

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

参考原文:https://blog.csdn.net/qq_38499671/article/details/109237698

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)