显示最多沙发次数的人的列表
一直想弄一个显示本博客沙发次数最多的来访者的列表,这样可以更大的激发来访者的评论热情,虽然不知道这样做的效果到底会不会很好,但是还是值得一试!
这个文章的标题响了好久,都没有准确的表达出来!具体的意思就是:
获取在博客上沙发次数最多的人的信息,并将其显示出来!分为状元,榜样,探花,实际上就是对 WordPress 的 Comment 表进行查询,获取抢沙发最多的三个留言者并将它们显示出来即可。
程序的代码非常简单,基本的逻辑如下:
1. 查出含有留言的 Post 的 ID。
2. 然后找出它们的第一条留言的留言者和其博客,帮把这些信息写到一个数组中。
3. 对数组进行处理找出抢到沙发的三个留言者。
4. 输出他们。
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 ?>
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 函数即可。
具体的实例大家可以看我的这个页面:点击查看





我爱水煮鱼有做过这个插件,还深入分析了效率问题
想法非常不错
请教:如果我只想显示文章的沙发评论者,那应该怎么写呢?
@keelii 这个网上有很多很多啦~呵呵
@xiao3 我怎么就没找到呢?