WP中文工具箱更新(一)

Wordpress 诞生于英语环境,因此由于计算机对语言支持的区别,它对中文的支持在细节上不尽人意。我们常常借助桑椹的中文工具箱来解决这个问题。但即便最近的版本也距今整整两年了,需要对它进行一些升级。

我们设计主题时,常常喜欢在侧边栏放置最新评论的模块,中文工具箱的 get_recent_comments 函数能够胜任这个工作。但在某些特定的场合,例如出现包含超链接的评论,尤其 get_recent_comments 中 $cut 的数值完全将超链接长度包含进去时,我们的网页将可能会出现畸形。因为具有不少字符的超链接被认为是一个单词,即使长度超过我们在 css 定义的容器宽度,系统也不会自动换行,造成超链接撑破框架的难看模样。

我的解决办法是,设置一个判断参数,假若回复中含有某个字段,则调整 $cut 来实现输出评论内容长度的控制。具体如下:

打开 mulberrykit.php,将如下代码替换 get_recent_comments 函数。

  1. function get_recent_comments($no_comments = 5, $before = '<li> ', $after = '</li>', $show_pass_post = false, $cut=0, $string_filter = 'http') {
  2.  
  3.   global $wpdb;
  4.   $request = "SELECT ID, comment_ID, comment_content, comment_author FROM $wpdb->posts, $wpdb->comments WHERE $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'";
  5.  
  6.   if(!$show_pass_post) { $request .= "AND post_password ='' "; }
  7.  
  8.   $request .= "AND comment_approved = '1' ORDER BY $wpdb->comments.comment_date DESC LIMIT $no_comments";
  9.   $comments = $wpdb->get_results($request);
  10.   $output = '';
  11.   foreach ($comments as $comment) {
  12.     $comment_author = stripslashes($comment->comment_author);
  13.     $comment_content = strip_tags($comment->comment_content);
  14.     $comment_content = stripslashes($comment_content);
  15.     if(strstr($comment_content,$string_filter))
  16.     {
  17.       $comment_excerpt =substr($comment_content,0,20);
  18.     }   
  19.     else
  20.     {
  21.       $comment_excerpt =substr($comment_content,0,$cut);
  22.     }
  23.     $comment_excerpt = utf8_trim($comment_excerpt);
  24.     $permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
  25.     $output .= $before . '<a href="' . $permalink . '" title="View the entire comment by ' . $comment_author . '">' . $comment_author . '</a>: ' . $comment_excerpt . '...' . $after;
  26.   }
  27.   echo $output;
  28. }

$string_filter 默认值为 http。也就是当评论中含有 http 字样时,会执行

  1. $comment_excerpt =substr($comment_content,0,20);

这个语句,实现只显示20个字节的评论内容。当然,如果评论不包含 http,则按照正常 $cut 输出内容长度。

你可以修改 “20″ 以达到理想的状态。

如此方法略微简陋,但至少解决了问题。

Tags:, .
评论数量(0) | Add Comments
本文网址:http://www.21percent.com.cn/qyll/html/2008/1057.html
最受大家欢迎的文章:http://www.21percent.com.cn/qyll/tags
一起出行?OR 了解情况?请Gmail联系:
Tsian's Gmail

There are No comments.

» You can leave a response or Trackback .

leave a reply