甜·咖啡
Software Wordpress || ProE SolidWorks
rss
email
twitter
facebook
  • Funny
  • Google
  • JustPicture
  • media
  • phone
  • Software
  • System
  • Web
  • Wordpress

显示最多沙发次数的人的列表

5条评论 | 162 次浏览
Posted on 2009-09-07 by xiao3

一直想弄一个显示本博客沙发次数最多的来访者的列表,这样可以更大的激发来访者的评论热情,虽然不知道这样做的效果到底会不会很好,但是还是值得一试!

这个文章的标题响了好久,都没有准确的表达出来!具体的意思就是:

获取在博客上沙发次数最多的人的信息,并将其显示出来!分为状元,榜样,探花,实际上就是对 WordPress 的 Comment 表进行查询,获取抢沙发最多的三个留言者并将它们显示出来即可。

程序的代码非常简单,基本的逻辑如下:

1. 查出含有留言的 Post 的 ID。
2. 然后找出它们的第一条留言的留言者和其博客,帮把这些信息写到一个数组中。
3. 对数组进行处理找出抢到沙发的三个留言者。
4. 输出他们。

具体的代码就是下面这样:

PHP语言: 获取 最多沙发党 函数
01 <!– 获取 最多沙发党  函数 –>
02 <?php
03 function sofa(){
04     global $wpdb;
05
06     $first_commentors = array(); //初始化沙发党数组
07
08     $q = "SELECT DISTINCT comment_post_id FROM $wpdb->comments WHERE comment_type ='' AND user_id = 0 AND comment_approved = ‘1′";
09     $have_comment_post_ids = $wpdb->get_results($q); //获取有留言的日志ID
10     foreach ($have_comment_post_ids as $have_comment_post_id){
11         $q = "SELECT comment_author FROM $wpdb->comments WHERE comment_type ='' AND user_id = 0 AND comment_approved = ‘1′ AND comment_post_id = $have_comment_post_id->comment_post_id order by comment_date limit 1";
12         $first_comment = $wpdb->get_results($q);  //获取沙发党
13         array_push($first_commentors,$first_comment[0] -> comment_author); //添加进沙发党数组
14     }
15
16     $first_commentors = (array_count_values ($first_commentors)); //统计
17     arsort($first_commentors); //排序
18
19     $first_commentors_author = array_keys($first_commentors);//获取沙发党名字
20
21     $output = ""; //初始化输出字符串
22     $output .= '<ul class="wp_sofa">';
23     for($i=0; $i<3; $i++){
24         $title="";
25         if($i==0)$title = "状元:";
26         if($i==1)$title = "榜眼:";
27         if($i==2)$title = "探花:";
28         $q = "SELECT comment_author_url FROM $wpdb->comments WHERE comment_type ='' AND user_id = 0 AND comment_approved = ‘1′ AND comment_author_url !=” AND comment_author = ‘$first_commentors_author[$i]' limit 1";
29         $first_comment_url = $wpdb->get_results($q); //获取沙发党的博客
30         if($first_comment_url){
31             $output .= '<li><a href="' . $first_comment_url[0] -> comment_author_url . '" title="' . $first_commentors_author[$i].'">' . $title.$first_commentors_author[$i] . '</a>(' . $first_commentors["$first_commentors_author[$i]"].')</li>';
32         } else {
33             $output .= '<li>' . $title.$first_commentors_author[$i] . '('.$first_commentors["$first_commentors_author[$i]"] . ')</li>';
34         }
35     }
36     $output .= '</ul>';
37     echo $output;//输出沙发党前三甲
38 }
39 ?>

将上面的代码放进你博客主题的 functions.php 文件里面就可以啦!最后就是怎么将其显示出来~这就只需要一句代码,很简单:

1 <?php if(function_exists('sofa')) { sofa(); } ?>

在模板上(一般是在侧边栏侧或者单独页面)调用上面这句代码,显示这个 sofa 函数即可。

具体的实例大家可以看我的这个页面:点击查看

欢迎订阅: Google Reader | 鲜果 | 抓虾 | 九点 | QQ邮箱 | 有道 | 更多...

  Tags: PHP, 沙发 Category: Wordpress

5 Comments

  1. LiGht 说:
    2009年09月8日于1:25 下午

    我爱水煮鱼有做过这个插件,还深入分析了效率问题

    回复
  2. hongmop 说:
    2009年09月14日于4:32 下午

    想法非常不错

    回复
  3. keelii 说:
    2010年03月10日于9:48 上午

    请教:如果我只想显示文章的沙发评论者,那应该怎么写呢?

    回复
  4. xiao3 说:
    2010年03月11日于12:43 下午

    @keelii 这个网上有很多很多啦~呵呵 :sad:

    回复
  5. keelii 说:
    2010年03月11日于3:04 下午

    @xiao3 我怎么就没找到呢?

    回复

Leave a Reply

点击这里取消回复


订阅

    Rss Feed Twitter @ mengzehe

相关文章

  • 网页版飞信客户端(PHP版)
  • 2010世界杯网页插件
  • Wordpress 免插件输出最新 Twitter
  • Wordpress自动输入个人信息javascript标签
  • 腾出来一些东西

最近文章

  • 为您的 Google App Engine 应用提供免费的加速服务
  • 性感美女演唱Twitter之歌
  • 申请属于自己的免费Twitter Api
  • Google Chrome Web Apps
  • 老大哥在看着你
  • 美国队赢啦
  • 2010南非世界杯各国海报——ESPN出品
  • Taobao For Chrome 淘宝及支付宝功能插件

热评文章

  • 成功搜到t.qq.com的invite (91)
  • 放出奇艺邀请50枚 (51)
  • 免费高速代理软件Puff (33)
  • 看图不说话【4】 (25)
  • 免翻墙观看Youtube视频(Firefox 和 Chrome) (21)
  • 《我的身体,我的心 - 初学者的 SM 指导》中文版 (21)
  • Wordpress评论栏优化概括 (19)
  • 使用jQuery为非站内超链接添加图标 (16)

最近评论

分类存档


  • 投稿
  • 合作
  • 存档
  • Links
  • 订阅
  • ghs
  • 关于
  • 登录
Powered by Wordpress  |  Designed by WebTreats