不论投机还是投资

偶尔也会迷茫 ——GUYAHN顾亚恒 2015-2023

WordPress 获取文章的评论人数方法

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

为什么我说是“变态需求”呢?因为折腾WP主题这么久,我从来没接触过/听说过有人需要获取文章的评论人数,对,评论人数!不是评论数,同一个人发1条、10条、100条评论也只能算1人。

WordPress 获取文章的评论人数方法

既然没人需要或者很少人需要,那么就难于找到相关方法了,我也查看过WordPress相关函数,木有这类直接的输出或者简单就能获取“评论人数”的方法/函数。

/* 获取文章的评论人数 */
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($post->ID); ?>

调用方法:

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

 

 

点赞
  1. Avatar photo 顾亚恒(GuYahn)说道:

    代码很好用,我已经使用了

  2. 那个小鸟不会飞说道:

    我之前也在找这个问题的解决方案

  3. 怎样网说道:

    不用插件实现这个挺好,我收藏了,过阵子也用上,您首页的幻灯片效果挺炫的啊~~

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据