如何实现WordPress读者墙按评论等级显示相应读者?

我们国内很多WordPress主题都会带有一个读者墙功能,Nana主题也不一例外,但是看多了按总评论数的排行榜,是否有点腻歪?而且我们大多数主题都有评论等级的功能,是否想过读者按评论等级逐级显示出来?今天就教大家如何实现WordPress读者墙按评论等级显示相应读者。

以Nana主题为例,找到template-readers文件,复制一份并重名为template-pinglunbang,然后打开该文件,找到以下代码

Template Name: 读者百强榜

修改为

Template Name: 评论榜单

继续找到以下代码

<?php
function readers_wall($limit = "100")
{
	global $wpdb;
	$counts = $wpdb->get_results("SELECT count(comment_author) AS cnt, comment_author, comment_author_url, comment_author_email FROM $wpdb->comments WHERE user_id!='1' AND comment_author!='1' AND comment_approved='1' AND comment_type='' GROUP BY comment_author ORDER BY cnt DESC LIMIT $limit");
	$i = 0;
    $type='';
	foreach ($counts as $count ) {
		$i++;
		$c_url = $count->comment_author_url;

		if (!$c_url) {
			$c_url = "https://boke112.com";
		}

		$tt = $i;

		if ($i == 1) {
			$tt = "读者之青龙";
		}
		else if ($i == 2) {
			$tt = "读者之白虎";
		}
		else if ($i == 3) {
			$tt = "读者之朱雀";
		}
		else if ($i == 4) {
			$tt = "读者之玄武";
		}
		else {
			$tt = "第" . $i . "名";
		}
		$avatar = my_avatar( $count->comment_author_email,36,$default='',$count->comment_author);
		if ($i < 5) {
			$type .= "<a class=\"item-top item-" . $i . "\" target=\"_blank\" href=\"" . $c_url . "\" title=\"【" . $tt . "】评论:" . $count->cnt . "\"><h4>【" . $tt . "】</h4>".$avatar."<strong>" . $count->comment_author . "</strong>" . $c_url . "</a>";
		}
		else {
			$type .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"【" . $tt . "】评论:" . $count->cnt . "\">" . $avatar . $count->comment_author . "</a>";
		}
	}

	echo $type;
}
?>
<div class="readers">
<?php readers_wall(100);?>
</div>

修改为

<?php
function readers_wall($limit = 100)
{
	global $wpdb;
    $counts = $wpdb->get_results("SELECT count(comment_author_email) AS cnt, comment_author, comment_author_url, comment_author_email FROM $wpdb->comments WHERE user_id!='1' AND comment_approved='1' AND comment_type='' AND comment_author_url!='' GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 0 , $limit");
	$i1 = 0;$i2 = 0;$i3 = 0;$i4 = 0;$i5 = 0;$i6 = 0;$i7 = 0;$i8 = 0;
	$dj = 0;
	$xiucai = '';
	$juren = '';
	$jinshi = '';
	$zhuangyuan = '';
	$shangshu = '';
	$chengxiang = '';
	$wangye = '';
	$huangdi = '';
	foreach ($counts as $count ) {
		$dj=$count->cnt;
		$c_url = $count->comment_author_url;
		$avatar = my_avatar( $count->comment_author_email,36,$default='',$count->comment_author);
		if($dj >= 20 && $dj < 40){
		$i1++;			
		$xiucai .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"评论量 " . $count->cnt . " 条,名列【秀才榜】第" . $i1 . "位\" target=\"_blank\" rel=\"external nofollow\">" . $avatar . $count->comment_author . "</a>";
		}
		if($dj >= 40 && $dj < 80){ 
		$i2++;
		$juren .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"评论量 " . $count->cnt . " 条,名列【举人榜】第" . $i2 . "位\" target=\"_blank\" rel=\"external nofollow\">" . $avatar . $count->comment_author . "</a>";
		}
		if($dj >= 80 && $dj < 160){  
		$i3++;
		$jinshi .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"评论量 " . $count->cnt . " 条,名列【进士榜】第" . $i3 . "位\" target=\"_blank\" rel=\"external nofollow\">" . $avatar . $count->comment_author . "</a>";
		}
		if($dj >= 160 && $dj < 320){  
		$i4++;
		$zhuangyuan .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"评论量 " . $count->cnt . " 条,名列【状元榜】第" . $i4 . "位\" target=\"_blank\" rel=\"external nofollow\">" . $avatar . $count->comment_author . "</a>";
		}
		if($dj >= 320 && $dj < 640){ 
		$i5++; 
		$shangshu .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"评论量 " . $count->cnt . " 条,名列【尚书榜】第" . $i5 . "位\" target=\"_blank\" rel=\"external nofollow\">" . $avatar . $count->comment_author . "</a>";
		}
		if($dj >= 640 && $dj < 1280){  
		$i6++;
		$chengxiang .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"评论量 " . $count->cnt . " 条,名列【丞相榜】第" . $i6 . "位\" target=\"_blank\" rel=\"external nofollow\">" . $avatar . $count->comment_author . "</a>";
		}
		if($dj >= 1280 && $dj < 2560){  
		$i7++;
		$wangye .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"评论量 " . $count->cnt . " 条,名列【王爷榜】第" . $i7 . "位\" target=\"_blank\" rel=\"external nofollow\">" . $avatar . $count->comment_author . "</a>";
		}
		if($dj >= 2560 && $dj < 100000){ 
		$i8++; 
		$huangdi .= "<a target=\"_blank\" href=\"" . $c_url . "\" title=\"评论量 " . $count->cnt . " 条,名列【皇帝榜】第" . $i8 . "位\" target=\"_blank\" rel=\"external nofollow\">" . $avatar . $count->comment_author . "</a>";
		}
	}
if(!empty($xiucai)){$xiucai ="<h2 style=\"border-left: 6px solid #FF0000;background-color: #FFD790;\"><strong>读者评论等级排行榜(秀才榜)</strong></h2><div class=\"readers\">".$xiucai."</div>";}
if(!empty($juren)){$juren ="<h2 style=\"border-left: 6px solid #FF0000;background-color: #FFD780;\"><strong>读者评论等级排行榜(举人榜)</strong></h2><div class=\"readers\">".$juren."</div>";}
if(!empty($jinshi)){$jinshi ="<h2 style=\"border-left: 6px solid #FF0000;background-color: #FFD770;\"><strong>读者评论等级排行榜(进士榜)</strong></h2><div class=\"readers\">".$jinshi."</div>";}
if(!empty($zhuangyuan)){$zhuangyuan ="<h2 style=\"border-left: 6px solid #FF0000;background-color: #FFD760;\"><strong>读者评论等级排行榜(状元榜)</strong></h2><div class=\"readers\">".$zhuangyuan."</div>";}
if(!empty($shangshu)){$shangshu ="<h2 style=\"border-left: 6px solid #FF0000;background-color: #FFD750;\"><strong>读者评论等级排行榜(尚书榜)</strong></h2><div class=\"readers\">".$shangshu."</div>";}
if(!empty($chengxiang)){$chengxiang ="<h2 style=\"border-left: 6px solid #FF0000;background-color: #FFD740;\"><strong>读者评论等级排行榜(丞相榜)</strong></h2><div class=\"readers\">".$chengxiang."</div>";}
if(!empty($wangye)){$wangye ="<h2 style=\"border-left: 6px solid #FF0000;background-color: #FFD720;\"><strong>读者评论等级排行榜(王爷榜)</strong></h2><div class=\"readers\">".$wangye."</div>";}
	if(!empty($huangdi)){$huangdi ="<h2 style=\"border-left: 6px solid #FF0000;background-color: #FFD700;\"><strong>读者评论等级排行榜(皇帝榜)</strong></h2><div class=\"readers\">".$huangdi."</div>";}	
	echo $huangdi;echo $wangye;echo $chengxiang;echo $shangshu;echo $zhuangyuan;echo $jinshi;echo $juren;echo $xiucai;
}
readers_wall(120);?>

保存上传到主题文件。

PS:其中评论的等级名称及相对应的评论数量请自行修改即可。

后台新建一个页面,模板就选我们刚才新创建的“评论榜单”然后直接修好好固定链接之后直接发布即可。