Skip to content


让WordPress的最近评论Widget直接显示评论内容

WordPress自带的最近评论Widgts显示的效果是 someone on postname,但不少人希望让它显示为someone says: something。修改WordPress的/includes/widgets.php可以达到目的。

打开widgets.php文件,首先添加一个自定义函数my_utf8_trim()(取于WordPress中文工具箱),用于截断从数据库取出来的评论字符串。

  1. function my_utf8_trim($str)
  2. {
  3. $len = strlen($str);
  4. for ($i=strlen($str)-1; $i>=0; $i-=1)
  5. {
  6. $hex .= ' '.ord($str[$i]);
  7. $ch = ord($str[$i]);
  8. if (($ch & 128)==0) return(substr($str,0,$i));
  9. if (($ch & 192)==192) return(substr($str,0,$i));
  10. }
  11. return($str.$hex);
  12. }

接下来修改函数wp_widget_recent_comments($args)(以WordPress 2.3.2 为例):

  1. function wp_widget_recent_comments($args)
  2. {
  3. global $wpdb, $comments, $comment;
  4. extract($args, EXTR_SKIP);
  5. $options = get_option('widget_recent_comments');
  6. $title = empty($options['title']) ? __('Recent Comments') : $options['title'];
  7. if ( !$number = (int) $options['number'] )
  8. $number = 5;
  9. else if ( $number < 1 )
  10. $number = 1;
  11. else if ( $number > 15 )
  12. $number = 15;
  13. if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) )
  14. {
  15. $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
  16. wp_cache_add( 'recent_comments', $comments, 'widget' );
  17. }
  18. ?>
  19. <?php echo $before_widget; ?>
  20. <?php echo $before_title . $title . $after_title; ?>
  21. <ul id="recentcomments"><?php
  22. if ( $comments ) : foreach ($comments as $comment) :
  23. echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
  24. endforeach; endif;?></ul>
  25. <?php echo $after_widget; ?>
  26. <?php
  27. }

将其整体整改为:

  1. function wp_widget_recent_comments($args)
  2. {
  3. global $wpdb, $comments, $comment;
  4. extract($args, EXTR_SKIP);
  5. $options = get_option('widget_recent_comments');
  6. $title = empty($options['title']) ? __('Recent Comments') : $options['title'];
  7. if ( !$number = (int) $options['number'] )
  8. $number = 5;
  9. else if ( $number < 1 )
  10. $number = 1;
  11. else if ( $number > 15 )
  12. $number = 15;
  13. if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
  14. $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID,comment_content FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
  15. wp_cache_add( 'recent_comments', $comments, 'widget' );
  16. }
  17. ?>
  18. <?php echo $before_widget; ?>
  19. <?php echo $before_title . $title . $after_title; ?>
  20. <ul id="recentcomments"><?php
  21. if ( $comments ) : foreach ($comments as $comment) :
  22. $comment_content = strip_tags($comment->comment_content);
  23. $comment_content = stripslashes($comment_content);
  24. $comment_content = preg_replace('/\[qu(.(?!\[\/quote]))+.\[\/quote]/si', '', $comment_content);
  25. $comment_content = preg_replace('/\s*:em\d\d:\s*/si', '', $comment_content);
  26. $comment_excerpt =substr($comment_content,0,50);
  27. $comment_excerpt = my_utf8_trim($comment_excerpt);
  28. echo  '<li class="recentcomments">' . sprintf(__('%1$s:%2$s'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . $comment_excerpt .'...'. '</a>') . '</li>';
  29. endforeach; endif;?></ul>
  30. <?php echo $after_widget; ?>
  31. <?php
  32. }

最后说一句,记得把文件另存为UTF8格式,因为我给出的修改代码中有一个中文冒号:)

Popularity: 37% [?]

相关日志:

Posted in 电脑网络. Tagged with , .

12 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. 要改源码,太麻烦了,有没有现成的插件?

    对了,正在找个好的代码显示插件,你用的是什么?

  2. Leo said

    @WebGuru:没有现成的插件,过段时间有空的话我看能不能捣鼓一个出来,自己也觉得每次升级都改源码很麻烦。
    我用的是CoolCode

  3. KHui said

    呵呵,还有更简单的哦。直接在sidebar.php添加这一段代码就可以了,不过有一点bug就是有的字体显示方框,怎么回事,希望楼主能帮忙解决下。
    我放出代码
    ---------------------------------------
    Recent Comments

    comments
    LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    $wpdb->posts.ID)
    WHERE comment_approved = '1' AND comment_type = '' AND
    post_password = ''
    ORDER BY comment_date_gmt DESC
    LIMIT 10";
    $comments = $wpdb->get_results($sql);

    $output = $pre_HTML;
    $output .= "\n";
    foreach ($comments as $comment) {

    $output .= "\n".strip_tags($comment->comment_author)
    .":" . "ID) .
    "#comment-" . $comment->comment_ID . "\" title=\"on " .
    $comment->post_title . "\">" . strip_tags($comment->com_excerpt)
    ."
    ";

    }
    $output .= "\n";
    $output .= $post_HTML;

    echo $output;?>

  4. Leo said

    @KHui:

    没错,确实可以这样实现,不过这样的话换次主题就要修改一次sidebar.php。

    把链接给我看看。

  5. 先评论,再说,呵呵,比较喜欢这个主题,跟我的有些神似,但是布局更漂亮,呵呵

  6. Wyatt said

    请问,你边栏的“最新评论”用的是什么插件啊?

  7. 中国挪动 said

    能不能这样显示
    XX在XX主题写下XXXXXX

  8. 湘湘 said

    我正在为这个东东烦脑,哎,我是菜鸟,看起来很复杂,不知道能否把它弄好

Continuing the Discussion

  1. NATSON 记事 linked to this post on 2008-06-02

    直接显示wordpress的评论...

    每次更新都TNND要改一遍,郁闷,到处上网找方案,干脆直接贴过来存档
    author : Leo Space
    修改WordPress的/includes/widgets.php可以达到目的。
    打开widgets.php文件,首先添加一个自定义函数my_utf8_trim()(....

  2. 替换wordpress默认的widgets : : 轶侠的网上小窝 linked to this post on 2008-06-16

    [...] 起因:让WordPress的最近评论Widget直接显示评论内容,而那个Get Recent Comments插件功能太多,太庞大。我用不着,看着好好的wp默认的widget浪费掉于心不忍,又不想改源代码,懒得写插件。 [...]

  3. 博狗 » 让widget显示评论内容 - 博客世界,有博乃大 linked to this post on 2008-08-02

    [...] 对于wordpress的widget功能确实强大,可以自由的定制sitebar显示的内容。wordpress内置的几个widget都蛮好用,不过有点郁闷的是显示最新评论的功能。在sitebar中仅能显示出评论者以及所评论的相应文章,并不能显示出评论的内容。在看过leo所写的让WordPress的最近评论Widget直接显示评论内容一文后,Johnny感觉是蛮不错,可惜是要修改不少代码,这样一来,对于菜鸟级别的是具有一定难度,再者wordpress每次升级后,都得重新修改这部分代码,如果忘记修改了又不能实现该功能。所以johnny想想有没更简单的方式来实现。接触过中文wordpress工具箱的同学都应该知道它有一个功能就是可以显示最新的留言,而其显示的方式正是直接显示评论内容,只可惜用到该功能必须将PHP代码添加到模板中,又是得改代码的事,不方便。其实只要让widget能运行PHP,一切都可以解决了。 [...]

  4. 花渐渐的博客 » Blog Archive » WordPress最近评论Widget直接显示评论内容 linked to this post on 2008-12-11

    [...] 那就用这个链接,这位老兄贴的方法比我好。链接地址 [...]

Some HTML is OK

(never shared)

or, reply to this post via trackback.


SEO Powered by Platinum SEO from Techblissonline