wordpress处理the_excerpt的代码,你可以在wp-includes目录下的formatting.php中找到,通过代码,你可发现the_excerpt是用空格来计算长度的,这对英文是可以的,但是对中文,基本上不可行。
formatting.php中的代码如下:
01 | function wp_trim_excerpt($text = '') { |
02 | $raw_excerpt = $text; |
03 | if ( '' == $text ) { |
04 | $text = get_the_content(''); |
05 |
06 | $text = strip_shortcodes( $text ); |
07 |
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 ); |
13 | } |
14 | return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); |
15 | } |
中文文章的长度问题就是出在$text = implode(‘ ‘, $words);
解决方式
在您自己主题下面的functions.php文件中添加如下代码就可以解决了
1 | function excerpt_read_more_link($output) { |
2 | global $post; |
3 | $output = mb_substr($output,0, 200); |
4 | return $output . '<span><a href="'. get_permalink($post->ID).'">阅读全文...</a></span>'; |
5 | } |
6 | add_filter('the_excerpt', 'excerpt_read_more_link'); |
快看看您的文章吧,效果还不错吧。
本文介绍了解决WordPress中使用the_excerpt生成中文文章摘要时遇到的问题,通过修改默认代码以适应中文字符长度,确保摘要准确且不被截断。
1260

被折叠的 条评论
为什么被折叠?



