假装more截断的wordpress首页摘要输出

LMS
1.1K+ 3

虽然使用mb_substr截断文字来获得wordpress很方便,但看起来实在太呆板了,显得很傻。如果使用wordpress自带的excerpt()配合excerpt_length来自动显示摘要,当里面有英文的时候,好像字数上的控制很难把握,而且遇到短代码貌似就不输出摘要了。

可是我很不喜欢用more标签啊。小骆驼商队的Betty童鞋给我们整了一个 适合中文的 WordPress 摘要插件 东西很好用,可以像more标签那样输出摘要,可是不适合我的主题——她将single页面使用的excerpt也给摘要了。于是我给改了改,改的更简单了。

if ( !function_exists('mb_strlen') ) {
        function mb_strlen ($text, $encode) {
                if ($encode=='UTF-8') {
                        return preg_match_all('%(?:
                                          [\x09\x0A\x0D\x20-\x7E]           # ASCII
                                        | [\xC2-\xDF][\x80-\xBF]            # non-overlong 2-byte
                                        |  \xE0[\xA0-\xBF][\x80-\xBF]       # excluding overlongs
                                        | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
                                        |  \xED[\x80-\x9F][\x80-\xBF]       # excluding surrogates
                                        |  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
                                        | [\xF1-\xF3][\x80-\xBF]{3}         # planes 4-15
                                        |  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
                                        )%xs',$text,$out);
                }else{
                        return strlen($text);
                }
        }
}
if (!function_exists('mb_substr')) {
    function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
        $limit = strlen($str);
        for ($s = 0; $start > 0;--$start) {// found the real start
            if ($s >= $limit)
                break;
            if ($str[$s] <= "\x7F")
                ++$s;
            else {
                ++$s; // skip length
                while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
                    ++$s;
            }
        }
        if ($len == '')
            return substr($str, $s);
        else
            for ($e = $s; $len > 0; --$len) {//found the real end
                if ($e >= $limit)
                    break;
                if ($str[$e] <= "\x7F")
                    ++$e;
                else {
                    ++$e;//skip length
                    while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
                        ++$e;
                }
            }
        return substr($str, $s, $e - $s);
    }
}
function wp_new_excerpt($text){
        global $post;
        $text = $post->post_excerpt;//如果摘要有内容直接用摘要
        if ($text == ''){
                $text = $post->post_content;
                $text = strip_shortcodes($text);
                $text = str_replace(']]>', ']]>', $text);
                $text = trim($text);

                $more_position = stripos ($text, "<!--more-->");//兼容more截断
                if ($more_position !== false) {
                        $text = substr ($text, 0, $more_position);
                        return $text;
                }
                $length = '160';//摘要输出的长度
                $allowd_tag = '<p><h1><h2><h3><h4><h5><h6><blockquote><a>';//忽略的html标签
                $strip_short_post = true;
                if(($length > mb_strlen(strip_tags($text), 'utf-8')) && ($strip_short_post === true) ) {
                        $text = strip_tags($text, $allowd_tag);                 
                        $text = trim($text);
                        return $text;
                }
                $text = strip_tags($text, $allowd_tag);                 
                $text = trim($text);
                $num = 0;
                $in_tag = false;
                for ($i=0; $num<$length || $in_tag; $i++) {
                        if(mb_substr($text, $i, 1) == '<')
                                $in_tag = true;
                        elseif(mb_substr($text, $i, 1) == '>')
                                $in_tag = false;
                        elseif(!$in_tag)
                                $num++;
                }
                $text = mb_substr ($text,0,$i, 'utf-8');
                $text = trim($text);
                return $text;
        }
        return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wp_new_excerpt');

前面两个是重新定义 mb_strlenmb_substr 函数,可以忽略,摘要输出函数是 wp_new_excerpt

这个函数的使用和betty同学的有些区别,修改过的函数只对主题模板中的 the_excerpt() 起作用,对 the_content() 不起作用,所以要输出摘要的地方都得换成 the_excerpt()

修改后的代码不是插件形式,使用时丢在functions.php文件里就行。

THE END

评论 3

    1. 从别人插件改过来的,有许多不如意,自己又搞不定。

发表评论

Submit