WordPress自带的最近评论Widgts显示的效果是 someone on postname,但不少人希望让它显示为someone says: something。修改WordPress的/includes/widgets.php可以达到目的。
打开widgets.php文件,首先添加一个自定义函数my_utf8_trim()(取于WordPress中文工具箱),用于截断从数据库取出来的评论字符串。
- function my_utf8_trim($str)
- {
- $len = strlen($str);
- for ($i=strlen($str)-1; $i>=0; $i-=1)
- {
- $hex .= ' '.ord($str[$i]);
- $ch = ord($str[$i]);
- if (($ch & 128)==0) return(substr($str,0,$i));
- if (($ch & 192)==192) return(substr($str,0,$i));
- }
- return($str.$hex);
- }
接下来修改函数wp_widget_recent_comments($args)(以WordPress 2.3.2 为例):
- function wp_widget_recent_comments($args)
- {
- global $wpdb, $comments, $comment;
- extract($args, EXTR_SKIP);
- $options = get_option('widget_recent_comments');
- $title = empty($options['title']) ? __('Recent Comments') : $options['title'];
- if ( !$number = (int) $options['number'] )
- $number = 5;
- else if ( $number < 1 )
- $number = 1;
- else if ( $number > 15 )
- $number = 15;
- if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) )
- {
- $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");
- wp_cache_add( 'recent_comments', $comments, 'widget' );
- }
- ?>
- <?php echo $before_widget; ?>
- <?php echo $before_title . $title . $after_title; ?>
- <ul id="recentcomments"><?php
- if ( $comments ) : foreach ($comments as $comment) :
- 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>';
- endforeach; endif;?></ul>
- <?php echo $after_widget; ?>
- <?php
- }
将其整体整改为:
- function wp_widget_recent_comments($args)
- {
- global $wpdb, $comments, $comment;
- extract($args, EXTR_SKIP);
- $options = get_option('widget_recent_comments');
- $title = empty($options['title']) ? __('Recent Comments') : $options['title'];
- if ( !$number = (int) $options['number'] )
- $number = 5;
- else if ( $number < 1 )
- $number = 1;
- else if ( $number > 15 )
- $number = 15;
- if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
- $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");
- wp_cache_add( 'recent_comments', $comments, 'widget' );
- }
- ?>
- <?php echo $before_widget; ?>
- <?php echo $before_title . $title . $after_title; ?>
- <ul id="recentcomments"><?php
- 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 '<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>';
- endforeach; endif;?></ul>
- <?php echo $after_widget; ?>
- <?php
- }
最后说一句,记得把文件另存为UTF8格式,因为我给出的修改代码中有一个中文冒号:)
Popularity: 37% [?]
相关日志:
12 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
要改源码,太麻烦了,有没有现成的插件?
对了,正在找个好的代码显示插件,你用的是什么?
@WebGuru:没有现成的插件,过段时间有空的话我看能不能捣鼓一个出来,自己也觉得每次升级都改源码很麻烦。
我用的是CoolCode
呵呵,还有更简单的哦。直接在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;?>
@KHui:
没错,确实可以这样实现,不过这样的话换次主题就要修改一次sidebar.php。
把链接给我看看。
先评论,再说,呵呵,比较喜欢这个主题,跟我的有些神似,但是布局更漂亮,呵呵
请问,你边栏的“最新评论”用的是什么插件啊?
能不能这样显示
XX在XX主题写下XXXXXX
我正在为这个东东烦脑,哎,我是菜鸟,看起来很复杂,不知道能否把它弄好
Continuing the Discussion