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

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

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

1、制作评论榜单模板。以 Nana 主题为例,找到 template-readers 文件,复制一份并重名为 template-pinglunbang,然后打开该文件,找到以下代码

Template Name: 读者百强榜

修改为

Template Name: 评论榜单

继续找到以下代码

  1. <?php
  2. function readers_wall($limit = “100”)
  3. {
  4.     global $wpdb;
  5.     $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”);
  6.     $i = 0;
  7.     $type=”;
  8.     foreach ($counts as $count ) {
  9.         $i++;
  10.         $c_url = $count->comment_author_url;
  11.         if (!$c_url) {
  12.             $c_url = “https://boke112.com”;
  13.         }
  14.         $tt = $i;
  15.         if ($i == 1) {
  16.             $tt = “读者之青龙”;
  17.         }
  18.         else if ($i == 2) {
  19.             $tt = “读者之白虎”;
  20.         }
  21.         else if ($i == 3) {
  22.             $tt = “读者之朱雀”;
  23.         }
  24.         else if ($i == 4) {
  25.             $tt = “读者之玄武”;
  26.         }
  27.         else {
  28.             $tt = “第” . $i . “名”;
  29.         }
  30.         $avatar = my_avatar( $count->comment_author_email,36,$default=”,$count->comment_author);
  31.         if ($i < 5) {
  32.             $type .= “<a  . $i . “” target=”_blank” href=”” . $c_url . “” title=”【” . $tt . “】评论:” . $count->cnt . “”><h4>【” . $tt . “】</h4>”.$avatar.“<strong>” . $count->comment_author . “</strong>” . $c_url . “</a>”;
  33.         }
  34.         else {
  35.             $type .= “<a target=”_blank” href=”” . $c_url . “” title=”【” . $tt . “】评论:” . $count->cnt . “”>” . $avatar . $count->comment_author . “</a>”;
  36.         }
  37.     }
  38.     echo $type;
  39. }
  40. ?>
  41. <div class=“readers”>
  42. <?php readers_wall(100);?>
  43. >

</div

修改为

  1. <?php
  2. function readers_wall($limit = 100)
  3. {
  4.     global $wpdb;
  5.     $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”);
  6.     $i1 = 0;$i2 = 0;$i3 = 0;$i4 = 0;$i5 = 0;$i6 = 0;$i7 = 0;$i8 = 0;
  7.     $dj = 0;
  8.     $xiucai = ”;
  9.     $juren = ”;
  10.     $jinshi = ”;
  11.     $zhuangyuan = ”;
  12.     $shangshu = ”;
  13.     $chengxiang = ”;
  14.     $wangye = ”;
  15.     $huangdi = ”;
  16.     foreach ($counts as $count ) {
  17.         $dj=$count->cnt;
  18.         $c_url = $count->comment_author_url;
  19.         $avatar = my_avatar( $count->comment_author_email,36,$default=”,$count->comment_author);
  20.         if($dj >= 20 && $dj < 40){
  21.         $i1++;
  22.         $xiucai .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【秀才榜】第” . $i1 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
  23.         }
  24.         if($dj >= 40 && $dj < 80){
  25.         $i2++;
  26.         $juren .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【举人榜】第” . $i2 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
  27.         }
  28.         if($dj >= 80 && $dj < 160){
  29.         $i3++;
  30.         $jinshi .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【进士榜】第” . $i3 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
  31.         }
  32.         if($dj >= 160 && $dj < 320){
  33.         $i4++;
  34.         $zhuangyuan .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【状元榜】第” . $i4 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
  35.         }
  36.         if($dj >= 320 && $dj < 640){
  37.         $i5++;
  38.         $shangshu .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【尚书榜】第” . $i5 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
  39.         }
  40.         if($dj >= 640 && $dj < 1280){
  41.         $i6++;
  42.         $chengxiang .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【丞相榜】第” . $i6 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
  43.         }
  44.         if($dj >= 1280 && $dj < 2560){
  45.         $i7++;
  46.         $wangye .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【王爷榜】第” . $i7 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
  47.         }
  48.         if($dj >= 2560 && $dj < 100000){
  49.         $i8++;
  50.         $huangdi .= “<a target=”_blank” href=”” . $c_url . “” title=”评论量 “ . $count->cnt . ” 条,名列【皇帝榜】第” . $i8 . “位” target=”_blank” rel=”external nofollow”>” . $avatar . $count->comment_author . “</a>”;
  51.         }
  52.     }
  53. if(!empty($xiucai)){$xiucai =“<h6 ><strong>读者评论等级排行榜(秀才榜)</strong></h6><div >”.$xiucai.“>”;}
  54. if(!empty($juren)){$juren =“<h6 ><strong>读者评论等级排行榜(举人榜)</strong></h6><div >”.$juren.“>”;}
  55. if(!empty($jinshi)){$jinshi =“<h6 ><strong>读者评论等级排行榜(进士榜)</strong></h6><div >”.$jinshi.“>”;}
  56. if(!empty($zhuangyuan)){$zhuangyuan =“<h6 ><strong>读者评论等级排行榜(状元榜)</strong></h6><div >”.$zhuangyuan.“>”;}
  57. if(!empty($shangshu)){$shangshu =“<h6 ><strong>读者评论等级排行榜(尚书榜)</strong></h6><div >”.$shangshu.“>”;}
  58. if(!empty($chengxiang)){$chengxiang =“<h6 ><strong>读者评论等级排行榜(丞相榜)</strong></h6><div >”.$chengxiang.“>”;}
  59. if(!empty($wangye)){$wangye =“<h6 ><strong>读者评论等级排行榜(王爷榜)</strong></h6><div >”.$wangye.“>”;}
  60.     if(!empty($huangdi)){$huangdi =“<h6 ><strong>读者评论等级排行榜(皇帝榜)</strong></h6><div >”.$huangdi.“>”;}
  61.     echo $huangdi;echo $wangye;echo $chengxiang;echo $shangshu;echo $zhuangyuan;echo $jinshi;echo $juren;echo $xiucai;
  62. }
  63. readers_wall(120);?>

</div

保存并更新文件。

PS:以上代码中的评论的等级名称及相对应的评论数量请自行修改即可。</div

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

你可能感兴趣的文章
  • 优化WordPress评论楼层号,可同时支持顺序倒序排列
  • 纯代码给WordPress侧边栏小工具添加幻灯片功能
  • 为WordPress主题导航菜单添加个性图标字体
  • 出现Failed AddprinterrDriver2错误解决办法
  • 南宁江南大道科目三电子考详细步骤
  • 广西广电网络机顶盒和宽带网上如何缴费?
  • 如何实现zblogPHP站点自动添加图片alt属性
  • 给WordPress文章图片添加Fancybox 3灯箱效果教程

</p

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-05-10 13:54:42
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: