网站建设

WordPress酷炫CSS3读者墙,排名按年度、本月、本周划分的小方法

Jager · 10月28日 · 2014年 · 3715次已读

WordPress 酷炫 CSS3 读者墙这个玩意一般不用我多说,大部分用 WordPress 的博主都了解过了,出自折子戏博客。

不过他这个读者墙的排行是按年度划分的,也就是一年内的留言数排行。为了增强评论的积极性,我们可以学习卢松松留言板的按月份排行的做法。甚至还可以一周为单位,进一步激励读者评论留言。WordPress酷炫CSS3读者墙,排名按年度、本月、本周划分的小方法

下面分享修改排名时间段的方法。

先看下原代码中的 MySQL 查询代码:

$query="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE comment_date > date_sub( NOW(), INTERVAL 24 MONTH ) AND user_id='0' AND comment_author_email != '改成你的邮箱账号' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 39";

大家可以看到查询代码中有一个 comment_date > date_sub( NOW(), INTERVAL 24 MONTH )的关键性约束,也就是 24 个月的评论排行。那了解了这一点,事情就好办了!

根据 mysql 时间段条件约束,有如下语句可选:

①、将 comment_date 字段的时间限制在本年度范围:

where comment_date between date_sub(now(),interval 1 year) and now()

②、将 comment_date 字段的时间限制在本月范围:

where date_format(comment_date,'%Y-%m')=date_format(now(),'%Y-%m')

③、将 comment_date 字段时间限制在本周范围(从周日算起):

where yearweek(date_format(comment_date,'%Y-%m-%d')) = yearweek(now())

所以,正在在使用这个读者墙的朋友,如果要改变排行时间段,只要参考①②③,修改读者墙代码中的

where comment_date > date_sub( NOW(), INTERVAL 24 MONTH )

即可,非常简单!

Ps:大家也看到了,为什么年度排行和本周排行我用的是 1 YEAR,和 1 WEEK,而不是 24 MONTH 和 7 DAY 呢?其实是存在一个逻辑性问题的:使用 24 MONTH 或 7 DAY 来作为时间段划分,逻辑上是存在问题的,因为这个时间段的起点是活动的,没法固定的!

比如,今天的近 7 天和明天的近 7 天是不一样的,这样查询的数据也就不准确了!所以,我采用了 1 YEAR 和 1 WEEK,这样的好处就是明确的规定了时间段的起点,比如本周这个时间段,那起点就是上周日。

好了,各时段的代码都已给出,下面贴上一周为时间段的完整代码:

①、本周排行的完整代码

<!-- start 读者墙  Edited By iSayme-->
<style type="text/css">
.readers-list{line-height:18px;text-align:left;overflow:hidden;_zoom:1}
.readers-list li{width:200px;float:left;*margin-right:-1px}
.readers-list a,.readers-list a:hover strong{background-color:#f2f2f2;background-image:-webkit-linear-gradient(#f8f8f8,#f2f2f2);background-image:-moz-linear-gradient(#f8f8f8,#f2f2f2);background-image:linear-gradient(#f8f8f8,#f2f2f2)}
.readers-list a{position:relative;display:block;height:36px;margin:4px;padding:4px 4px 4px 44px;color:#999;overflow:hidden;border:#ccc 1px solid;border-radius:2px;box-shadow:#eee 0 0 2px}
.readers-list img,.readers-list em,.readers-list strong{-webkit-transition:all .2s ease-out;-moz-transition:all .2s ease-out;transition:all .2s ease-out}
.readers-list img{width:36px;height:36px;float:left;margin:0 8px 0 -40px;border-radius:2px}
.readers-list em{color:#666;font-style:normal;margin-right:10px}
.readers-list strong{color:#ddd;width:40px;text-align:right;position:absolute;right:6px;top:4px;font:bold 14px/16px microsoft yahei}
.readers-list a:hover{border-color:#bbb;box-shadow:#ccc 0 0 2px;background-color:#fff;background-image:none}
.readers-list a:hover img{opacity:.6;margin-left:0}
.readers-list a:hover em{color:#EE8B17;font:bold 12px/36px microsoft yahei}
.readers-list a:hover strong{color:#EE8B17;right:150px;top:0;text-align:center;border-right:#ccc 1px solid;height:44px;line-height:40px}
</style>
< ?php
//下面是评论查询语句,可通过修改 INTERVAL 1 WEEK 来改变时间段:
    $query="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE  yearweek(date_format(comment_date,'%Y-%m-%d')) = yearweek(now()) AND user_id='0' AND comment_author_email != '改成你的邮箱账号' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 39";//大家把管理员的邮箱改成你的,最后的这个 39 是选取多少个头像,大家可以按照自己的主题进行修改,来适合主题宽度
    $wall = $wpdb->get_results($query);
    $maxNum = $wall[0]->cnt;
    foreach ($wall as $comment)
    {
        $width = round(40 / ($maxNum / $comment->cnt),2);//此处是对应的血条的宽度
        if( $comment->comment_author_url )
        $url = $comment->comment_author_url;
        else $url="#";
		$avatar = get_avatar( $comment->comment_author_email, $size = '36', $default = get_bloginfo('wpurl').'/avatar/default.jpg' );
        $tmp = "<li><a target="_blank" href="".$comment->comment_author_url."">".$avatar."<em>".$comment->comment_author."</em> <strong>+".$comment->cnt."</strong>".$comment->comment_author_url."</a></li>";
        $output .= $tmp;
     }
    $output = "<ul class="readers-list">".$output."</ul>";
    echo $output ;
?>
<!-- end 读者墙 -->

将以上代码添加到主题目录的留言板模块的合适位置即可,如果你还没有留言板,那么请自行百度如何给 Wrodpress 添加留言板,具体做法我就不赘述了。

如果,你需要其他时间段的排行,那么只要参考本文前面分享的时间段,修改下代码中的INTERVAL 1 WEEK即可!

当然,张戈博客一直以分享为宗旨,所以本站留言板同时存在多个时间段排行的完整代码如下,仅供参考:

<!-- 张戈博客修改版读者墙排行代码 -->
<style type="text/css">
.readers-list{line-height:18px;text-align:left;overflow:hidden;_zoom:1}
.readers-list li{width:200px;float:left;*margin-right:-1px}
.readers-list a,.readers-list a:hover strong{background-color:#f2f2f2;background-image:-webkit-linear-gradient(#f8f8f8,#f2f2f2);background-image:-moz-linear-gradient(#f8f8f8,#f2f2f2);background-image:linear-gradient(#f8f8f8,#f2f2f2)}
.readers-list a{position:relative;display:block;height:36px;margin:4px;padding:4px 4px 4px 44px;color:#999;overflow:hidden;border:#ccc 1px solid;border-radius:2px;box-shadow:#eee 0 0 2px}
.readers-list img,.readers-list em,.readers-list strong{-webkit-transition:all .2s ease-out;-moz-transition:all .2s ease-out;transition:all .2s ease-out}
.readers-list img{width:36px;height:36px;float:left;margin:0 8px 0 -40px;border-radius:2px}
.readers-list em{color:#666;font-style:normal;margin-right:10px}
.readers-list strong{color:#ddd;width:40px;text-align:right;position:absolute;right:6px;top:4px;font:bold 14px/16px microsoft yahei}
.readers-list a:hover{border-color:#bbb;box-shadow:#ccc 0 0 2px;background-color:#fff;background-image:none}
.readers-list a:hover img{opacity:.6;margin-left:0}
.readers-list a:hover em{color:#EE8B17;font:bold 12px/36px microsoft yahei}
.readers-list a:hover strong{color:#EE8B17;right:150px;top:0;text-align:center;border-right:#ccc 1px solid;height:44px;line-height:40px}
</style>
<h2>年度评论排行 TOP3</h2>
<?php   
    $query1="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE comment_date between date_sub(now(),interval 1 year) and now() AND user_id='0' AND comment_author_email != '改成你的邮箱账号' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 3";   
    $wall = $wpdb->get_results($query1);    
    $maxNum = $wall[0]->cnt;   
    foreach ($wall as $comment)   
    {   
        $width = round(40 / ($maxNum / $comment->cnt),2);   
        //此处是对应的血条的宽度   
        if( $comment->comment_author_url )    
          $url = $comment->comment_author_url;    
        else $url="#"; 
  $avatar = get_avatar( $comment->comment_author_email, $size = '32', $default = get_bloginfo('wpurl').'/avatar/default.jpg' );    
        $tmp = "<li><a rel=\"friend\" alt=\"avatar 头像\" target=\"_blank\" href=\"".$comment->comment_author_url."\">".$avatar."<em>".$comment->comment_author."</em> <strong>+".$comment->cnt."</strong></br>".$comment->comment_author_url."</a></li>";   
        $output1 .= $tmp;   
     }    
    $output1 = "<ul class=\"readers-list\">".$output1."</ul>";    
    echo $output1 ;   
?>    
<!-- end 年度排行 -->  
<div class="clear"></div>			
<br />
<h2>本月评论排行 TOP6</h2>
<?php   
    $query2="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE date_format(comment_date,'%Y-%m')=date_format(now(),'%Y-%m') AND user_id='0' AND comment_author_email != '改成你的邮箱账号' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 6";   
    $wall = $wpdb->get_results($query2);    
    $maxNum = $wall[0]->cnt;   
    foreach ($wall as $comment)   
    {   
        $width = round(40 / ($maxNum / $comment->cnt),2);   
        //此处是对应的血条的宽度   
        if( $comment->comment_author_url )    
          $url = $comment->comment_author_url;    
        else $url="#"; 
  $avatar = get_avatar( $comment->comment_author_email, $size = '32', $default = get_bloginfo('wpurl').'/avatar/default.jpg' );    
        $tmp = "<li><a rel=\"friend\" alt=\"avatar 头像\" target=\"_blank\" href=\"".$comment->comment_author_url."\">".$avatar."<em>".$comment->comment_author."</em> <strong>+".$comment->cnt."</strong></br>".$comment->comment_author_url."</a></li>";   
        $output2 .= $tmp;   
     }    
    $output2 = "<ul class=\"readers-list\">".$output2."</ul>";    
    echo $output2 ;   
?>    
<!-- end 本月排行 --> 
<div class="clear"></div>			
<br />
<h2>本周评论排行 TOP9</h2>
<?php   
    $query3="SELECT COUNT(comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID) WHERE yearweek(date_format(comment_date,'%Y-%m-%d')) = yearweek(now()) AND user_id='0' AND comment_author_email != '改成你的邮箱账号' AND post_password='' AND comment_approved='1' AND comment_type='') AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 9";   
    $wall = $wpdb->get_results($query3);    
    $maxNum = $wall[0]->cnt;   
    foreach ($wall as $comment)   
    {   
        $width = round(40 / ($maxNum / $comment->cnt),2);   
        //此处是对应的血条的宽度   
        if( $comment->comment_author_url )    
          $url = $comment->comment_author_url;    
        else $url="#"; 
  $avatar = get_avatar( $comment->comment_author_email, $size = '32', $default = get_bloginfo('wpurl').'/avatar/default.jpg' );    
        $tmp = "<li><a rel=\"friend\" alt=\"avatar 头像\" target=\"_blank\" href=\"".$comment->comment_author_url."\">".$avatar."<em>".$comment->comment_author."</em> <strong>+".$comment->cnt."</strong></br>".$comment->comment_author_url."</a></li>";   
        $output3 .= $tmp;   
     }    
    $output3 = "<ul class=\"readers-list\">".$output3."</ul>";    
    echo $output3 ;   
?>    
<!-- end 本周排行·全部结束 -->

细看可以发现,其实组合三种时间段,最关键性的地方就修改变量名,否则一样的变量名会导致数据重复!需要修改的变量名是: output query,我是改成了 output1 output2 这种形式,相信你一看就懂!

最后,啰嗦一下,原文介绍是将 CSS 样式集成到 style.css 当中的,为啥我这直接和 php 写在一起呢?你想啊,如果是写到 style.css 那每个页面都需要加载一次,而写到页面代码中就只会在留言板加载,从而就起到优化的微小效果!当然,最好的方式可以将 css 代码保存为 readers.css。然后只添加到留言板当中即可,因为做出 css 文件,浏览器会缓存到本地,那就进一步优化了留言板的加载速度了(本段乃强迫症发作所述,可直接无视之,囧....)。

31 条回应
  1. 糯米汇 2014-10-28 · 18:03

    感觉不错 我去试试

  2. 微信大学 2014-10-28 · 19:08

    已成功!感谢Jager.
    但是评论者的头像,有的不显示. 提示504

    • avatar
      Jager 2014-11-1 · 12:36

      注意代码中default.jpg

  3. 成都SEO 2014-10-28 · 21:01

    好炫的功能。有空了用用

    • 叶德华 2014-10-31 · 22:10

      飞哥,你怎么那么多别名啊!

  4. 龙三公子 2014-10-28 · 21:04

    zblog的没有插件,上次看了下,要搞有些麻烦,不然我也折腾一个。

    • avatar
      Jager 2014-11-1 · 12:32

      自己写代码

  5. 梦轩丽人 2014-10-29 · 12:59

    不错的功能,等下班后回家再试试,谢谢分享。

  6. ysicing 2014-10-29 · 13:16

    签到成功!签到时间:下午1:15:08,每日签到,生活更精彩哦~

  7. 小马博客 2014-10-29 · 16:34

    这功能不错!我去试试!

  8. 中国历史 2014-10-29 · 16:59

    按 @梦轩丽人 指示,特来感谢Jager,我修改了读者墙中的排名,按一周评论倒序排

    • avatar
      Jager 2014-11-1 · 12:32

      哈哈,都改了,不错的

  9. 中国历史 2014-10-29 · 19:05

    话说Jager自己搬石头砸脚,他懂的

  10. 灰常记忆 2014-10-29 · 19:08

    额。有空折腾下。

  11. 收录独立博客 2014-10-30 · 14:45

    wp这个程序一直在用。哈哈,折腾人的是技术。

  12. 飞哥 2014-10-30 · 19:12

    感觉是个好东西,,就是代码加载有点麻烦。对于我们小白来讲实现起来有点难度啊、不过还是要认可博主的认真劲

  13. 飞哥 2014-10-30 · 19:13

    这个代码是不是可以用一下呢?我去试试!

  14. 糯米汇 2014-10-31 · 12:38

    通过梦轩丽人博主帮助,已经弄上去了

    • avatar
      Jager 2014-11-1 · 12:34

      不错不错

  15. 叶德华 2014-10-31 · 22:09

    唉,看你们一个个的都这么爱折腾博客,给博客加新功能,回头看看我自己的,就有种后妈生的的感觉。

    • avatar
      Jager 2014-11-1 · 12:34

      别偷懒就行。

  16. 我就看看,什么也不买 2014-11-1 · 10:21

    怎么百度收录的速度那么慢?

  17. 梦轩丽人 2014-11-3 · 11:53

    谢谢分享,已更改。PS:分享的本月评论代码有误,dateformat应该为date_format

    • avatar
      Jager 2014-11-3 · 12:14

      嗯,发现了,敲错了。

  18. avatar
    Jager 2014-11-30 · 15:59

    回来给你看下,应该简单。

    • avatar
      Jager 2014-12-1 · 10:39

      你自己应该可以写出来吧?都有案例了,直接扒代码,我这边已经偷过来了:https://zhang.ge/video/na-xie-nian/

      • avatar
        Jager 2014-12-1 · 12:49

        js是我额外改进的,判断其实一般主题都有,你看下主题下的comments.php就可以找到了

        • avatar
          Jager 2014-12-1 · 14:38

          QQ上说,这个没必要发文发教程。

  19. 滕绍武博客 2015-12-1 · 23:45

    有这个功能是不错,就是我是小白,不会用,直接用现成的

  20. 随谈网 2017-1-7 · 10:41

    挺炫的,我试了下把这个代码加到我博客里about页面,结果显示的每个血条之间间距好大,请博主帮忙看看是怎么回事,谢谢了

    • avatar
      Jager 2017-1-9 · 9:50

      看了下你的展示,将以下css加入到主题的自定义样式里面即可:

      .readers-list a:hover strong{right: 183px;}