分享一下,在网上看到的比较有意思的博客美化点,>> 留言墙 <<,将来访者按照评论数量为依据排序排版输出在页面上(重点是css样式真的很nice)
下面就是我博客的 留言墙(传送门,click here!)
以下就是实现代码,源代码是在 无主题 的博客看到的,据说原作者是 折子戏 ,在此先感谢各位大大慷慨分享了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $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 post_password='' AND comment_approved='1' AND comment_type='' ORDER BY comment_date DESC) AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 21"; //DESC LiMIT就是输出的评论者个数,可酌情修改 $wall = $wpdb->get_results($query); foreach ($wall as $comment){ if( $comment->comment_author_url ) $url = $comment->comment_author_url; else $url="#"; $avatar = get_avatar( $comment->comment_author_email, $size = '36' ); $tmp = "<li><a target=\"_blank\" href=\"".$comment->comment_author_url."\" rel=\"nofollow\">".$avatar."<em>".$comment->comment_author."</em> <strong>".$comment->cnt."条</strong><address>".$comment->comment_author_url."</address></a></li>"; $output .= $tmp; } $output = "<ul class=\"readers-list\">".$output."</ul>"; echo $output ; ?> |
代码的主要思路就是查询数据库 一定时间段的评论,并统计到每个人头上,最后按照评论者分组排序,这个学过sql语句的都应该知道,不会的也可以百度sql语句,如何设置一个时间区间(比如一年,一个月)然后替换上面查询语句的 date_sub( NOW(), INTERVAL 24 MONTH )部分,统计不同时间区间,就可以实现,年度排行榜,月排行榜神马的
当然这样输肯定不行,随话说 人靠衣装马靠鞍 ,给这个输出套上一个css样式也是很有必要的,不得不再说一次,这个css真的很nice
1 2 3 4 5 6 7 8 9 10 11 12 13 |
.readers-list{line-height:18px;text-align:left;overflow:hidden;_zoom:1;margin-bottom:20px} .readers-list li{width:185px;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:7px;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:50px;text-align:right;position:absolute;right:6px;top:4px;font:bold 14px/16px microsoft yahei} .readers-list address{overflow: hidden;whilte-space: nowrap;text-overflow: ellipsis;} .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:137px;top:0;text-align:center;border-right:#ccc 1px solid;height:44px;line-height:40px} |
这儿由于我的博客主题页面比较窄,我就吧标签缩短了一点,可以依博客主题,适度修整
如何应用到主题
代码都有了再来说说怎么用吧,开头的php代码一般放在主题的某个 模板页面 中,比如留言板(guestbook),注意不要放在调用评论的代码后面了,css代码放在主题的style.css最后就可以了
最后的代码,就是我修改后放在 模板留言板中的代码,实现了文章开头图片的样子,需要的可以参考参考,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<!--留言墙 2016/7/21.繁夜--> <h3>年度评论排行 TOP3</h3> <?php $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 between date_sub(now(),interval 1 year) and now() AND user_id='0' AND post_password='' AND comment_approved='1' AND comment_type='' ORDER BY comment_date DESC) AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 3"; $wall = $wpdb->get_results($query); foreach ($wall as $comment){ if( $comment->comment_author_url ) $url = $comment->comment_author_url; else $url="#"; $avatar = get_avatar( $comment->comment_author_email, $size = '36' ); $tmp = "<li><a target=\"_blank\" href=\"".$comment->comment_author_url."\" rel=\"nofollow\">".$avatar."<em>".$comment->comment_author."</em> <strong>".$comment->cnt."条</strong><address>".$comment->comment_author_url."</address></a></li>"; $output .= $tmp; } $output = "<ul class=\"readers-list\">".$output."</ul>"; echo $output ; ?> <div class="clear"></div><br /> <h3>本月评论排行 TOP6</h3> <?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 date_format(comment_date,'%Y-%m')=date_format(now(),'%Y-%m') AND user_id='0' AND post_password='' AND comment_approved='1' AND comment_type='' ORDER BY comment_date DESC) AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 6"; $wall = $wpdb->get_results($query1); foreach ($wall as $comment){ if( $comment->comment_author_url ) $url = $comment->comment_author_url; else $url="#"; $avatar = get_avatar( $comment->comment_author_email, $size = '36' ); $tmp = "<li><a target=\"_blank\" href=\"".$comment->comment_author_url."\" rel=\"nofollow\">".$avatar."<em>".$comment->comment_author."</em> <strong>".$comment->cnt."条</strong><address>".$comment->comment_author_url."</address></a></li>"; $output1 .= $tmp; } $output1 = "<ul class=\"readers-list\">".$output1."</ul>"; echo $output1 ; ?> <div class="clear"></div><br /> <h3>本周评论排行 TOP9</h3> <?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 yearweek(date_format(comment_date,'%Y-%m-%d')) = yearweek(now()) AND user_id='0' AND post_password='' AND comment_approved='1' AND comment_type='' ORDER BY comment_date DESC) AS tempcmt GROUP BY comment_author_email ORDER BY cnt DESC LIMIT 9"; $wall = $wpdb->get_results($query2); foreach ($wall as $comment){ if( $comment->comment_author_url ) $url = $comment->comment_author_url; else $url="#"; $avatar = get_avatar( $comment->comment_author_email, $size = '36' ); $tmp = "<li><a target=\"_blank\" href=\"".$comment->comment_author_url."\" rel=\"nofollow\">".$avatar."<em>".$comment->comment_author."</em> <strong>".$comment->cnt."条</strong><address>".$comment->comment_author_url."</address></a></li>"; $output2 .= $tmp; } $output2 = "<ul class=\"readers-list\">".$output2."</ul>"; echo $output2 ; ?> <div class="clear"></div> <!--留言墙结束--> |