WORDPRESS技巧| 2011年12月27日| ERA | 6,509 VIEWS
wordpress处理the_excerpt的代码,你可以在wp-includes目录下的formatting.php中找到,通过代码,你可发现the_excerpt是用空格来计算长度的,这对英文是可以的,但是对中文,基本上不可行。
formatting.php中的代码如下:
01 | function wp_trim_excerpt( $text = '' ) { |
04 | $text = get_the_content( '' ); |
06 | $text = strip_shortcodes( $text ); |
08 | $text = apply_filters( 'the_content' , $text ); |
09 | $text = str_replace ( ']]>' , ']]>' , $text ); |
10 | $excerpt_length = apply_filters( 'excerpt_length' , 55); |
11 | $excerpt_more = apply_filters( 'excerpt_more' , ' ' . '[...]' ); |
12 | $text = wp_trim_words( $text , $excerpt_length , $excerpt_more ); |
14 | return apply_filters( 'wp_trim_excerpt' , $text , $raw_excerpt ); |
中文文章的长度问题就是出在$text = implode(‘ ‘, $words);
解决方式
在您自己主题下面的functions.php文件中添加如下代码就可以解决了
1 | function excerpt_read_more_link( $output ) { |
3 | $output = mb_substr( $output ,0, 200); |
4 | return $output . '<span><a href="' . get_permalink( $post ->ID). '">阅读全文...</a></span>' ; |
6 | add_filter( 'the_excerpt' , 'excerpt_read_more_link' ); |
快看看您的文章吧,效果还不错吧。