中文wp中的the_content_rss调用问题
今天给朋友建立wordpress博客,优化模版时遇到这么一个问题。
该模版首页调用the_content_rss函数,并对输出内容大小进行限制。它是这么写的:the_content_rss(”,FALSE,”,50)。但是我发现post却是完全输出,并没有削去50个字符以后的内容。
几经折腾发现the_content_rss对内容大小限制的对象是word。再具体一点说就是以空格为界将内容划分为很多的word,然后输出the_content_rss需要调用的数目。
问题在于,就英文来说空格的确能作为区分word的依据。但对中文来说,除了标点很少有人会在句子中夹杂空格。因此对于中文post,the_content_rss几乎是输出全文的。
解决办法是修改feed.php中the_content_rss函数。为了不影响其他地方的调用。我们新建一个the_chinesecontent_rss。使用字符作为限制的对象,并再写utf8_trim函数清理中文字符截断可能产生的乱码。
具体代码如下:
- function the_chinesecontent_rss($more_link_text='(more...)', $stripteaser=0, $more_file='', $cut = 0, $encode_html = 0) {
- $content = get_the_content($more_link_text, $stripteaser, $more_file);
- $content = apply_filters('the_content_rss', $content);
- if ( $cut && !$encode_html )
- $encode_html = 2;
- if ( 1== $encode_html ) {
- $content = wp_specialchars($content);
- $cut = 0;
- }
- elseif ( 0 == $encode_html ) {
- $content = make_url_footnote($content);
- }
- elseif ( 2 == $encode_html ) {
- $content = strip_tags($content);
- }
- if ( $cut ) {
- $excerpt =substr($content,0,$cut);
- $excerpt = utf8_trim($excerpt);
- $content = $excerpt.'...';
- }
- $content = str_replace(']]>', ']]>', $content);
- echo $content;
- }
- function 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);
- }
下载该函数:
the_chinesecontent_rss.php (1.1 KiB, 62 hits)
Tags:WordPress.
评论数量(3) | Add Comments
本文网址:http://www.21percent.com.cn/qyll/html/2007/95.html
最受大家欢迎的文章:http://www.21percent.com.cn/qyll/tags
一起出行?OR 了解情况?请Gmail联系:


:)-辛苦啦~嘿嘿~
能做一个txt文件下载吗?我从上面拷贝下来以后再保存总是出错,自己又不懂代码,完全不知道哪里有问题。
搞定了,多谢提醒。