解决了WordPress sidebar widget中recent comments的显示问题

解决了SIDEBAR WIDGET中recent comments的显示问题

现在不会只显示"什么日志上面的什么人",而是会显示回复预览。

这么简单,改个sql,改两句显示语句就行了

重点是这个函数:

  1. <?php
  2. //hacked by JerryHong
  3. function wp_widget_recent_comments($args) {
  4.     global $wpdb, $comments, $comment;
  5.     extract($args, EXTR_SKIP);
  6.     $options = get_option('widget_recent_comments');
  7.     $title = empty($options['title']) ? __('Recent Comments') : $options['title'];
  8.     if ( !$number = (int) $options['number'] )
  9.         $number = 5;
  10.     else if ( $number < 1 )
  11.         $number = 1;
  12.     else if ( $number > 15 )
  13.         $number = 15;
  14.  
  15.     if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
  16.         $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");
  17.         wp_cache_add( 'recent_comments', $comments, 'widget' );
  18.     }
  19. ?>
  20.         <?php echo $before_widget; ?>
  21.             <?php echo $before_title . $title . $after_title; ?>
  22.             <ul id="recentcomments"><?php
  23.             if ( $comments ) : foreach ($comments as $comment) :
  24.             echo  '<li class="recentcomments">' . sprintf(__('%2$s--%1$s'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '" title='.$comment->comment_content .'>' . mb_substr($comment->comment_content,0,26,'UTF-8'). '</a>') . '</li>';
  25.             endforeach; endif;?></ul>
  26.         <?php echo $after_widget; ?>
  27. <?php
  28. }

Related posts

10 Responses to “解决了WordPress sidebar widget中recent comments的显示问题”


  1. 1 (沙发) qg

    你好!怎么让Recent Comments 、Recent Posts显示固定的字符,10字的仅仅让它显示5个 。应该修改哪个文件?我刚接触wp。

  2. 2 (板凳) Jerry

    @qg:
    wp-includes/widgets.php.
    WP默认的显示确实太丑了;)

  3. 3 (地毯) QG

    能告诉具体改一个语句吗?让它最新文章标题只显示15个字符,评论格式同你站一样!

  4. 4 (地板) Jerry

    在function wp_widget_recent_entries($args)里,试试substr(the_title(),0,15);
    若中文出现乱码可参考我文章里那个mb_substr()函数

  5. 5 (地下室) QG

    忘记跟你说了, 我php还未入门!根本就不知道substr(the_title(),0,15);应该放哪里?

  6. 6 (地基) Jerry

    服了……把这个载下来解压覆盖wp-includes\widgets.php
    http://www.JerryHong.com/wp-content/uploads/widgets.rar
    如果需要微调请改698行的那个15

  7. 7 (地壳) Jerry

    如果还有乱码……看来我解决不了了

  8. 8 (地幔) qg

    乱码没发生。但对数字截取敏感!后把GB2312改成UTF-8后就正常了!呵呵!thanks!

  9. 9 (地核) 江南游子

    我想问一下,如何让wp自带的widget中recent comments不要显示trackback和ping? 你可以访问我的站点,看看右上角,我只想显示comments, 但是wp自作主张地把trackback/ping都显示出来了。

    谢谢

  10. 10 (地球另一面) Jerry

    请修改wordpress/wp-includes/widgets.php的第878行

    1. $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");

    改成

    1. $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' AND comment_type<>'pingback' ORDER BY comment_date_gmt DESC LIMIT $number");

    应该就行了
    如果想显示评论内容请参考上面的附件……
    PS.域名不错

Comments are currently closed.