有个老实人问起叶子的收藏室如何实现侧边栏最新评论带Gravatar头像,叶子回答是因为本站使用的主题自带这个功能,老实人对此回答相当不满,叶子为了不得罪老实人,决定秉烛夜读,研究这一课题,经过查阅资料、实践论证、优选排除等几个环节,终于形成此文:

叶子教你四种方法实现WordPress侧边栏最新评论带Gravatar头像!

1.插件法(门槛低,推荐wp新手及懒人使用)

插件简介:
在 WordPress 的侧边栏显示最新评论. 你可以限制显示评论的数量, 设置评论摘要的最大长度, 过滤反链接评论. 你还可以显示或屏蔽评论者头像, 调整头像的尺寸和位置. 除此之外, 此插件支持 WordPress Widget. 而现在, 你可以通过点击分页按钮来查看新旧评论.
适用版本:
WordPress 2.5 或以上
插件下载: Download
安装方法:
1. 将压缩包解压到 {WordPress 根目录}/wp-content/plugins/
2. 登录你的 WordPress 后台, 到 Plugins 页面激活插件 WP-RecentComments
3. 这里有两种添加方法:
(1) 到 Design -> Widget, 将控件 WP-RecentComments 添加到你的侧边栏
(2) 打开 sidebar.php, 在适当位置添加以下代码:
[sourcecode language='php'][/sourcecode]

2.WordPress原生函数法(需要对wordpress程序代码有些了解,推荐wp熟手使用)
这个方法需要自己在sidebar.php文件的相应位置添加如下代码:

[sourcecode language='php'] $comments = get_comments('number=10');
foreach($comments as $comment) {
echo get_avatar( $comment, 32 ).
'

  • comment_post_ID).
    ‘ 上的评论”>’.get_comment_author().
    ‘ : ‘.$comment->comment_content.’
  • ‘;
    }
    ?>[/sourcecode]

    number=10 表示输出最近的10条评论
    get_avatar( $comment, 32 ) 表示输出头像,其中32为头像的宽度与高度
    get_comment_link() 是评论的链接地址
    $comment->comment_post_ID 是所评论文章的标题
    get_comment_author() 是评论作者
    $comment->comment_content 即为评论内容
    这个方法使用的代码较少,所以样式看起来可能不够美观,有基础的童鞋可以自己加以修饰。
    3.SQL查询语言法(涉及SQL语言,要读懂需要具备SQL语言知识,推荐wp老手使用)

    同样需要自己在sidebar.php文件的相应位置添加如下代码:

    [sourcecode language='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 .= $post_HTML;
    $output = convert_smilies($output);
    echo $output;
    ?>[/sourcecode]

    代码中的“你的用户名”请改成自己博客的用户名,作用是屏蔽自己恢复的评论出现在侧边栏,叶子的收藏室目前使用这一方法

    4.主题函数法(在主题的函数文件中注册widgets 应用,然后在侧边栏调用,推荐wp主题制作人员折腾)
    首先在主题支持函数文件functions.php中末尾“?>”的前面添加如下代码:
    [sourcecode language='php']function get_avatar_recent_comment() {

    global $wpdb;

    $sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID,
    comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved,
    comment_type,comment_author_url,
    SUBSTRING(comment_content,1,50) 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;
    $gravatar_status = ‘on’; /* off if not using */

    foreach ($comments as $comment) {

    $email = $comment->comment_author_email;
    $grav_name = $comment->comment_author_name;
    $grav_url = “http://www.gravatar.com/avatar.php?gravatar_id=”.md5($email). “&size=32″;
    ?>


  • ID); ?>#comment-comment_ID; ?>” title=”on post_title; ?>”>
    com_excerpt); ?>…
  • }
    }[/sourcecode]
    然后同样需要自己在sidebar.php文件的相应位置添加如下调用代码:

    [sourcecode language='php'][/sourcecode]

    四种方法提供给大家,想要这种格式的最新评论请自己选择一种,喜欢折腾的童鞋可以逐一试用,叶子没意见!本文叶子原创,欢迎转载,但请注明出处。



    相关日志

    目前有26条评论,期待您的参与…

    1. 小黎的IT博客 说道:

      用GOOGLE搜索到你的文章,,神奇啊,哈哈 我正在做个主题,谢谢叶子大哥啦

    2. 小黎的IT博客 说道:

      还有个问题在哪调头像大小,我用的是第三个方法

    3. winky 说道:

      厉害,果然够折腾~哈哈~发现搞WP真的会不知不觉陷进去的,没空的话还是要忍一忍。。。

    4. Andrea 说道:

      请问gravatar本来在我的首页,鼠标移动到上面就会跳出来一个框里面显示详细内容,今天我点了close hand什么什么,于是再不知道如何重新打开了。。。
      请高手指点!

    5. Nielsen 说道:

      有头像的人飘过OH~~~YEAH~!

    6. brushes8 说道:

      第二个 代码错误 第三个貌似也不对劲········

    你的博客想挣钱吗?新浪、网易的博客也可以的,详情点这里!

    那么,请您也说点什么吧…




    bottom-img