用wp-kit-cn的widget实现侧边栏读者墙

2010年5月22日 | 标签: , ,

    直接修改PHP代码就像直接调用Win32API,而插件就像包装好了的MFC,效率也许底了点但却更简单易用。我是个懒人,目前我的侧边栏都是用widget做的。昨天想添加个读者墙,查看了万戈、zww的代码,发现都是直接修改sidebar.php,而我无法把这个读者墙插到其他widget中间;试了一个hot friends插件,发现并不是很好使。

    其实wp-kit-cn提供的一堆widget中已有一个“WKC最近评论者”,显示最近一周或者一月留言最多的用户,只是显示的是用户名而不是gravatar头像。经过一番实验,我终于把它成功修改成显示头像,并且在FF和IE下都可以显示提示。

    原来使用了cache avatar插件,今天发现作者说缓存头像效果并不明显,同时由于修改需要,我现在停用了这个插件——这个插件好像并不是按一般的那个方式命名缓存头像文件,所以我把头像文件路径指到本地缓存没有成功,不过如果用get_avatar()函数获取头像,这个插件还是适用的。

  1. widget-recent-commentors.php
    这是直接实现这个widget的php文件,修改第38行,为<ul>添加class类别,方便后面通过css控制样式;添加<div class=”fixed”></div>,让头像并排显示(不懂~_~)。

    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    
    echo '<ul>'."\n";
    $param  = '';
    $param .= 'threshhold=' . $options['threshhold'];
    $param .= '&type=' . $options['type'];
    $param .= '&skipuser=' . $options['skipuser'];
    $param .= '&before=' . $options['before'];
    $param .= '&after=' . $options['after'];
    $param .= '&number=' . $options['number'];
    wkc_recent_commentors($param); 
    echo '</ul>'."\n";
    echo $after_widget;

    修改成

    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    echo '<ul class="ffox_most_active">'."\n";
    $param  = '';
    $param .= 'threshhold=' . $options['threshhold'];
    $param .= '&type=' . $options['type'];
    $param .= '&skipuser=' . $options['skipuser'];
    $param .= '&before=' . $options['before'];
    $param .= '&after=' . $options['after'];
    $param .= '&number=' . $options['number'];
    wkc_recent_commentors($param); 
    echo '</ul>'."\n";
    echo '<div class="fixed"></div>';
    echo $after_widget;
  2. wp-kit-cn.tags.php
    修改wkc_recent_commentors()函数,改变显示内容,从316行开始修改如下
    有一点要注意,这个文件时用Windows ANSI编码的,保存的时候要转换成UTF-8编码,否则会乱码—奇怪的是其他文件都直接是UTF-8的。

    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    
    $output .= $r['before'];
    $comment_author = htmlspecialchars(stripslashes($comment->comment_author));
    $comment_author_url =stripslashes($comment->comment_author_url);
    $comment_total = (int) $comment->comment_total;
    	if ($comment_author_url) {
    		$comment_author_link = "<a href='$comment_author_url' target='_blank'>$comment_author</a>";
    	} else {
    		$comment_author_link = "$comment_author";
    	}
    $output .= $comment_author_link . " ($comment_total)  ";
    $output .= $r['after'] . "\n";
    $no++;
     

    修改为

    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    
    $output .= '<li class="mostactive">'; //$r['before'];
    $comment_author = htmlspecialchars(stripslashes($comment->comment_author));
    $comment_author_url =stripslashes($comment->comment_author_url);
    $comment_total = (int) $comment->comment_total;
    	if ($comment_author_url) {
    		$comment_author_link = '<a href="' . $comment_author_url . '" title="' . $comment_author . ' (' . $comment_total . '条评论)" rel="external nofollow" target=_blank><img class="avatar avatar-40 photo" src="http://www.gravatar.com/avatar.php?gravatar_id=' . md5(strtolower($comment->comment_author_email)) . '&size=38&d=identicon&r=G" alt="' . $comment_author . ' ('. $comment_total . '条评论)" /></a>';
    	} else {
    		$comment_author_link = '<img class="avatar avatar-40 photo" src="http://www.gravatar.com/avatar.php?gravatar_id=' . md5(strtolower($comment->comment_author_email)) . '&size=38&d=identicon&r=G" alt="' . $comment_author . ' ('. $comment_total . '条评论)" />';
    	}
    $output .= $comment_author_link;
    $output .= '</li>' . "\n";
    $no++;
     
  3. wp-kit-cn.class.php
    直接操作数据库代码在这儿,修改get_recent_commentors()函数,为获取到的评论数据添加comment_author_email项
    第271行修改如下

    271
    272
    
    $query  = "SELECT comment_author, comment_author_url, COUNT(comment_ID) AS 'comment_total' 
     

    修改为

    271
    272
    
    $query  = "SELECT comment_author, comment_author_url, comment_author_email, COUNT(comment_ID) AS 'comment_total' 
     
  4. 主题文件夹下的style.css
    添加以下代码,按需自行修改吧

    1
    2
    3
    
    #sidebar .ffox_most_active li{ list-style:none; float:left; border:none; line-height:0; }
    #sidebar .ffox_most_active img.avatar{ width:38px; height:38px; border:1px solid #ddd;padding:0px;margin:0 1px 0 0;}
     

主要参考网页:WordPress 免插件读者墙 willin 版本 | ZWWoOoOo
                  WordPress 免插件读者墙修订版 » Life Studio
感谢skyoy同学全力相助^_^ 

zww那个显示的是最近30天的数据,而这个显示的是一周或一个月的数据。我选择的是week计数,现在刚到周日数据就重计了,这应该跟西方的习惯有关吧。
SQL那语句有点长,以后再研究,暂且搁置;另外wp-kit-cn还有个widget显示最近某些天的活跃读者,有时间也来改改。

声明:本站文章如无特别说明均为原创,转载请注明转自:用wp-kit-cn的widget实现侧边栏读者墙
  1. 2010年5月23日00:03

    自己折腾出来了,还写个教程,大大的好人啊。 :wink:
    很多用wp-kit-cn的,造福不少人哦。

  2. 2010年5月23日00:42

    不错,拜读了,Wepress要想有自己的粉还有很长路要走啊~~~ :cool:

    • 2010年5月23日00:47

      @樊文生 我看上一个叫FlatPress的,不需要数据库——PHP+MySQL,WordPress太强大了

  3. 2010年5月23日08:29

    HOHO,也很会折腾嘛

  4. 2010年5月23日10:42

    这插件不错。很有研究精神啊

  5. 2010年5月23日11:07

    待会也去折腾下

  6. 2010年5月23日11:08

    厉害啊

  7. 2010年5月23日13:06

    :grin: 这个功能我就需要了,影响速度!嘿嘿

  8. 2010年5月23日14:02

    @zhy
    重新计数才好哦。

  9. 2010年5月23日20:46

    这方法独特,这个插件我只用了一个功能,显示随机日志。 :evil:

  10. 2010年5月24日23:21

    我要榜上有名~~

  11. 2010年5月25日00:01

    @朵未 没办法的事啊,因为没办法控制插入位置。。

  12. 2010年8月19日17:32

    wp-kit-cn我在模板边栏插入此代码来显示最新评论,我想问下怎么修改wp-kit-cn源文件才能够显示评论者的头像呢avatar?

  13. 2011年4月8日17:30

    为什么我按照教程去做还是不显示呢?最后那个style.css代码放在什么位置呢?我是小白