用wp-kit-cn的widget实现侧边栏读者墙
直接修改PHP代码就像直接调用Win32API,而插件就像包装好了的MFC,效率也许底了点但却更简单易用。我是个懒人,目前我的侧边栏都是用widget做的。昨天想添加个读者墙,查看了万戈、zww的代码,发现都是直接修改sidebar.php,而我无法把这个读者墙插到其他widget中间;试了一个hot friends插件,发现并不是很好使。
其实wp-kit-cn提供的一堆widget中已有一个“WKC最近评论者”,显示最近一周或者一月留言最多的用户,只是显示的是用户名而不是gravatar头像。经过一番实验,我终于把它成功修改成显示头像,并且在FF和IE下都可以显示提示。

原来使用了cache avatar插件,今天发现作者说缓存头像效果并不明显,同时由于修改需要,我现在停用了这个插件——这个插件好像并不是按一般的那个方式命名缓存头像文件,所以我把头像文件路径指到本地缓存没有成功,不过如果用get_avatar()函数获取头像,这个插件还是适用的。
- 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;
- 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++;
- 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'
- 主题文件夹下的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的,造福不少人哦。
@Skyoy 貌似现在重新计数了
不错,拜读了,Wepress要想有自己的粉还有很长路要走啊~~~
@樊文生 我看上一个叫FlatPress的,不需要数据库——PHP+MySQL,WordPress太强大了
HOHO,也很会折腾嘛
这插件不错。很有研究精神啊
@sslee 我的侧边栏三个都是用它的
待会也去折腾下
@Forsaken 我看你一会儿有副标题,一会儿没有啊
一直在后台修改着。。。又觉得没有副标题光秃秃的~
哪天想好了,我在友链里给你加上去
厉害啊
@joyla 应该是不需要吧,速度嘛并不是绝对的
@zhy
重新计数才好哦。
@Skyoy 我这“回复”左边有个隐藏的@,你发现了吗
这方法独特,这个插件我只用了一个功能,显示随机日志。
我要榜上有名~~
@ikeeptrying 呵呵,欢迎,我自己留言测试不小心占掉了两个位置
@朵未 没办法的事啊,因为没办法控制插入位置。。
wp-kit-cn我在模板边栏插入此代码来显示最新评论,我想问下怎么修改wp-kit-cn源文件才能够显示评论者的头像呢avatar?
@浩子 参见http://sexywp.com/wp-kit-cn-doc,最近评论Widget有相应的参数格式设置,看下就知道了
我放弃了那个插件了
直接用代码显示了
@浩子 嗯,代码也挺简单—
为什么我按照教程去做还是不显示呢?最后那个style.css代码放在什么位置呢?我是小白
@星雨镇Leo 不知道你那个“不显示”有个什么效果?‘, $comment_author.’ (‘.$comment_total.’条评论)’) . ‘ ‘;” 
最后那个样式直接添加到主题目录中style.css文件的任意位置就可以了
这个修改正确显示的关键是第2步,本文修改后的321行处我已经改为“$comment_author_link = ‘‘ . get_avatar($comment->comment_author_email, $size=’38′, $default=’
另外这个插件更新了,行号已经不对了~