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)

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格式

没有相关文章