重生西汉末年小说:让WordPress的最近评论Widget直接显示评论内容

来源:百度文库 编辑:偶看新闻 时间:2024/05/02 20:08:16

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. }

将其整体整改为:

  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. ?>
    • if ( $comments ) : foreach ($comments as $comment) :
    • $comment_content = strip_tags($comment->comment_content);
    • $comment_content = stripslashes($comment_content);
    • $comment_content = preg_replace('/\[qu(.(?!\[\/quote]))+.\[\/quote]/si', '', $comment_content);
    • $comment_content = preg_replace('/\s*:em\d\d:\s*/si', '', $comment_content);
    • $comment_excerpt =substr($comment_content,0,50);
    • $comment_excerpt = my_utf8_trim($comment_excerpt);
    • echo  '
    • ' . sprintf(__('%1$s:%2$s'), get_comment_author_link(), '' . $comment_excerpt .'...'. '') . '
    • ';
    • endforeach; endif;?>
  18. }

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