WordPress 侧边栏带头像的最新评论

谨以本文献给 Nox 童鞋。昨天 Nox 就问我怎么实现 WordPress 带头像的最新评论?因为当时正好在上班,电脑上没有 DW 和 notepad++ 这类软件,玩代码很不爽,又没有实力用 Windows 自带的记事本当场写出,所以就推荐了 zww 的成果:《带头像显示的最新评论代码 - 完善篇》,不过好像还是没有解决 Nox 的问题。现在也只有周末可以折腾折腾 WordPress 了,所以今天就拿自己的 WordPress 开刀,在侧边栏实现了带 gravatar 头像的最新评论。

先说说通用版:

<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,25) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND comment_author != '万戈' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output .= "\n<li>".get_avatar(get_comment_author_email('comment_author_email'), 18). " <a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"" . $comment->post_title . " 上的评论\">". strip_tags($comment->comment_author) .": ". strip_tags($comment->com_excerpt) ."</a></li>";
}
$output .= $post_HTML;
$output = convert_smilies($output);
echo $output;
?>

再说说 gravatar 头像缓存版的,大家各取所需啦:

<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,25) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND comment_author != '万戈' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output .= "\n<li><img width=\"18\" height=\"18\" src=\"http://wange.im/gravatar/cache/avatar/".md5(strtolower($comment->comment_author_email)). " \" /><a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"" . $comment->post_title . " 上的评论\">". strip_tags($comment->comment_author) .": ". strip_tags($comment->com_excerpt) ."</a></li>";
}
$output .= $post_HTML;
$output = convert_smilies($output);
echo $output;
?>

说明一下:代码中的“万戈”换成各自的名字就行,只是为了隐藏博主的最新留言,还有 gravatar 头像缓存的路径就根据各自的实际情况啦。

在完善 WordPress 带头像的最新评论的时候,最折腾我的就是样式,让我够累的,照顾了 Firefox,又忽略了 IE,特别是难缠的 IE6,我也是无能为力了,就先这么着吧,有时间再修改。

  1. Pingback: WordPress 非插件侧栏带头像最新评论 | 我要进步

  2. 感谢万大侠啊,不过就是我在每个头像之前还有2个实心句号,一个大,一个小,感觉只要留个小的就可以了,这是怎么回事情呢!请教了!

  3. Pingback: Wordpress侧栏无插件显示最新评论|空中楼阁

  4. Pingback: WordPress 非插件侧栏带头像最新评论 | 博客吧

  5. 万戈兄,对于这个调用最新评论,我有个建议兼疑问:假如设置显示的是5条评论,那能否实现5条评论中,每人只显示一条。因为有时候有些朋友会一连评论几条,这样这个栏目显示的都是他的留言了,看起来别扭。

    • 首先感谢万戈先生。貌似用wordpress时间不太长,但常常看到万戈的名字,谢谢这位强人。

      其次谢谢晓潘,改了之后,正常显示了。

  6. Pingback: 折腾之干掉插件疯狂篇 – 静夜燃香

  7. 万戈,你好~小弟新开了个Blog,很多东西都是学习你的博客的。看了这篇文章有一点不明白的,就是当评论的字数超过我所截取的字数时,我想有“...”表示,该怎么写?在这先谢过了 :smile:

  8. :sad: 我使用第一个代码,为什么显示出来的都是默认的头像?明明一些评论是有自己头像的,可是显示出来的所有都是默认的头像?为什么呢?

    • 任大侠,还记得三年前我还在用msn spaces的时候,你就给过我指点,铭记在心啊,再次来访,非常之鸡冻,欢迎啊~ :roll:

  9. 真是不错,我上次改好的显示评论者信息的文件代码还没传上去,近来校园网挂了,等放假回家再折腾。

  10. 哈哈,非常感谢啊。实现了,但是有个小问题。不知道是不是我CSS错误了,头像和评论会自动分行。。。

  11. 边看你博客边改,完成了两个改动.
    现在想请教另两个问题:
    1,评论邮件通知需要什么条件才可以实现,怎么实现?
    2,Ajax提交评论.
    都最好是代码实现.
    先行谢过.