让wordpress2.9X侧栏评论widget显示评论内容
最近修改主题的时候发现WordPress自带的最近评论Widgts添加后,显示的是“评论者名字 on 文章标题”,这样其实显示出来的意义就不大了,侧栏会出现很多相同的文字和链接,不仅看起来不方便,估计对SEO也没什么益处吧。所以想到让侧栏评论列表的WIDGET显示“评论者名字:评论内容”,具体修改方法如下:
打开wp-include文件夹,找到default-widget.php文件,首先添加如下函数代码:
{
$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);
}
然后找到下面的代码:
global $wpdb, $comments, $comment;
extract($args, EXTR_SKIP);
$title = apply_filters(‘widget_title’, empty($instance['title']) ? __(‘Recent Comments’) : $instance['title']);
if ( !$number = (int) $instance['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 $wpdb->comments.* FROM $wpdb->comments JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = ’1′ AND post_status = ‘publish’ ORDER BY comment_date_gmt DESC LIMIT 15″);
wp_cache_add( ‘recent_comments’, $comments, ‘widget’ );
}
$comments = array_slice( (array) $comments, 0, $number );
?>
-
if ( $comments ) : foreach ( (array) $comments as $comment) :
- ‘ . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x(‘%1$s on %2$s’, ‘widgets’), get_comment_author_link(), ‘
function widget($args, $instance ){
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’ );
}
?>
-
if ( $comments ) : foreach ($comments as $comment) :
- ‘ . sprintf(__(‘%1$s:%2$s’), get_comment_author_link(), ‘
$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 ‘
echo '



友情链接:
学习了,我试试,最好能够开发出一个插件。这样比较方便。
[回复]
微书摘 回复:
十月 10th, 2011 at 2:09 下午
插件虽然方便,但是会影响速度,所以还是尽量修改代码。
[回复]
BadJohnny 回复:
十月 11th, 2011 at 5:53 上午
其实如果你了解到插件的原理的时候,会发现,把插件的代码全部移植到主题中也可以,那么这样就算是修改代码了,也就是所谓的免插件,对吧?可实际上代码一点都没变少,该运行的还得运行,其实都一个道理而已。
[回复]