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

 懿古今   2022-10-02 17:42:09 更新  技术文档

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

如何实现WordPress读者墙按评论等级显示相应读者? - 第1张 - 懿古今(www.yigujin.cn)

1、制作评论榜单模板。以 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);?>

保存并更新文件(以上代码中的评论的等级名称及相对应的评论数量请自行修改即可)。

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

本文地址:https://www.yigujin.cn/1618.html

你可能感兴趣的文章

文章标签: ,   ,  
版权声明:本文为原创文章,版权归 懿古今 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请发邮件至[email protected],确认后马上更正、删除,谢谢!
淘宝和京东热销产品和VIP会员的优惠券推荐

发表评论

  1. maqingxi
    maqingxi @回复

    增加些娱乐性,挺好的。

    • 懿古今
      懿古今2018-05-14 16:46  回复

      @maqingxi[呲牙] 聊胜于无,喜欢的可以折腾,不过现在很少关注读者墙了

  2. 橘子书
    橘子书 @回复

    [阴险] 是不是没文章更新了? [挤眼]

  3. 广州网站建设
    广州网站建设 @回复

    不错不错,可以增加评论榜单

    • 懿古今
      懿古今2018-05-17 08:54  回复

      @广州网站建设弄一些比较个性的评论等级名称,加上适当的CSS,感觉还是很不错的,比起普通的读者墙更给力