其实也不怎么懂wordpress中的get_the_excerpt或the_excerpt,摘要是什么意思呢?截取文章的前一部分内容。当然中间要去除图片视频或html标签代码等。看摘要函数代码,发现其截取的是前55个词,注意是英文单词,而在中文中无此概念。且其区分单词的方法是数空格,就是数大约55个空格就算截到55个单词,如此函数,在处理中文内容时显示是错的,所以某英文模板截出来几乎整篇文章。。。
那么可以简单处理一下,用mb_substr函数截取前若干字符作为中文摘要即可。
function excerpt_link_modify2() { //为支持中文字符,修改获取摘要函数。 if ( '' == $text ) { $text = get_the_excerpt(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $text = mb_substr($text,0,200,'utf-8').'...'; //获取200个字符长作为摘要。 /* $excerpt_length = apply_filters('excerpt_length', 30); $words = explode(' ', $text, $excerpt_length + 1); if (count($words) > $excerpt_length) { array_pop($words); array_push($words, '...'); $text = implode(' ', $words); } } */ } echo $text; }
以上是某英文模板中的functions.php中的一个函数。将后面数英文单词的部分取消,直接用mb_substr截取utf-8字符串,200个字符长。
原创文章,作者:苏葳,如需转载,请注明出处:https://www.swmemo.com/452.html