先看下原代码中的 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 文件,浏览器会缓存到本地,那就进一步优化了留言板的加载速度了(本段乃强迫症发作所述,可直接无视之,囧….)。