缩略图+摘录=字数统计丢失

时间:2014-02-16 作者:Pieter Goosen

使用此函数显示我的摘录

 <?php   function get_the_content_limit_custom_allowedtags() {
// Add custom tags to this string
    return \'<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>\'; 
}

function pietergoosen_custom_wp_trim_excerpt($text) {
global $post;
$raw_excerpt = $text;
if ( \'\' == $text ) {
    $text = get_the_content(\'\');

    $text = strip_shortcodes( $text );

    $text = apply_filters(\'the_content\', $text);
    $text = str_replace(\']]>\', \']]&gt;\', $text);

    //Add the allowed HTML tags separated by a comma.
    $text = strip_tags($text, get_the_content_limit_custom_allowedtags());

    //Change the excerpt word count.
    if( get_post_type() == \'information\' ) {
        $excerpt_word_count = 55;
    } else {
    $excerpt_word_count = 85; 
    }
    $excerpt_length = apply_filters(\'excerpt_length\', $excerpt_word_count); 

    //Change the excerpt ending.
    $excerpt_end = \' <a href="\'. esc_url( get_permalink() ) . \'">\' . \'&hellip;\' . __( \'Read more about this article <span class="meta-nav">&rarr;</span>\', \'pietergoosen\' ) . \'</a>\'; 
    $excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end);

    $words = preg_split("/[\\n\\r\\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
        array_pop($words);
        $text = implode(\' \', $words);
        $text = $text . $excerpt_more;
    } else {
        $text = implode(\' \', $words);
    }
}
    return apply_filters(\'wp_trim_excerpt\', $text, $raw_excerpt);
}

remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter(\'get_the_excerpt\', \'pietergoosen_custom_wp_trim_excerpt\'); ?>
我面临的问题是,html标记会导致在使用the_excerpt(). 如果我在帖子的开头有一张图片,那么这篇帖子比没有图片的帖子要短20个字左右。我检查了几个解决方案,所有这些都归结为preg_slit() 作用我用找到的一对替换了代码中的一个,但他们把摘录搞砸了。我用过\'/(<a.*?a>)|\\n|\\r|\\t|\\s/\', \'|\\[(.+?)\\](.+?\\[/\\\\1\\])?|s\'. 有停止的想法吗the_excerpt() 将html标记计算为单词

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

对于那些正在寻找一个能够保持html标记的好摘录,并且希望该摘录不要打断句子中间部分,并且有一个真实的字数的人,下面是代码

 function pietergoosen_custom_wp_trim_excerpt($text) {
global $post;
$raw_excerpt = $text;
    if ( \'\' == $text ) {

        $text = get_the_content(\'\');
        $text = strip_shortcodes( $text );
        $text = apply_filters(\'the_content\', $text);
        $text = str_replace(\']]>\', \']]&gt;\', $text);

            //Add the allowed HTML tags separated by a comma.
            $allowed_tags = array(
            \'head\', \'title\', \'base\', \'link\', \'meta\', \'style\', \'script\', \'noscript\', \'body\', \'section\', \'nav\',
            \'article\', \'aside\', \'h1\', \'h2\', \'h3\', \'h4\', \'h5\', \'h6\', \'header\', \'footer\', \'address\', \'main\', \'p\', \'hr\',
            \'pre\', \'blockquote\', \'ol\', \'ul\', \'li\', \'dl\', \'dt\', \'dd\', \'figure\', \'figcaption\', \'div\', \'a\', \'em\', \'strong\',
            \'small\', \'s\', \'cite\', \'q\', \'dfn\', \'abbr\', \'data\', \'time\', \'code\', \'var\', \'samp\', \'kbd\', \'sub\', \'sup\', \'i\', \'b\',
            \'u\', \'mark\', \'ruby\', \'rt\', \'rp\', \'bdi\', \'bdo\', \'span\', \'br\', \'wbr\', \'ins\', \'del\', \'img\', \'iframe\', \'embed\',
            \'object\', \'param\', \'video\' ,\'audio\', \'source\', \'track\', \'canvas\', \'map\', \'area\', \'svg\', \'math\', \'table\',
            \'caption\', \'colgroup\', \'col\', \'tbody\', \'thead\', \'tfoot\', \'tr\', \'td\', \'th\', \'form\', \'fieldset\', \'legend\', \'label\',
            \'input\', \'button\', \'select\', \'datalist\', \'optgroup\', \'option\', \'textarea\', \'keygen\', \'output\', \'progress\', \'meter\',
            \'details\', \'summary\', \'menuitem\', \'menu\'
            );

            $tag_string = \'<\' . implode(\'><\', $allowed_tags) . \'>\';

        $text = strip_tags($text, $tag_string);

        //Set the excerpt word count and only break after sentence is complete.
            $excerpt_word_count = 75;
            $excerpt_length = apply_filters(\'excerpt_length\', $excerpt_word_count); 
            $tokens = array();
            $excerptOutput = \'\';
            $count = 0;

            // Divide the string into tokens; HTML tags, or words, followed by any whitespace
            preg_match_all(\'/(<[^>]+>|[^<>\\s]+)\\s*/u\', $text, $tokens);

            foreach ($tokens[0] as $token) { 

                if ($count >= $excerpt_word_count && preg_match(\'/[\\?\\.\\!]\\s*$/uS\', $token)) { 
                // Limit reached, continue until ? . or ! occur at the end
                    $excerptOutput .= trim($token);
                    break;
                }

                // Add words to complete sentence
                $count++;

                // Append what\'s left of the token
                $excerptOutput .= $token;
            }

        $text = trim(force_balance_tags($excerptOutput));

            $excerpt_end = \' <a href="\'. esc_url( get_permalink() ) . \'">\' . \'&nbsp;&raquo;&nbsp;\' . sprintf(__( \'Read more about: %s &nbsp;&raquo;\', \'pietergoosen\' ), get_the_title()) . \'</a>\'; 
            $excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end); 

            $pos = strrpos($text, \'</\');

        // Add \'Read more\' text inside last HTML tag
        $text = substr_replace($text, $excerpt_end, $pos, 0);

    }
    return apply_filters(\'wp_trim_excerpt\', $text, $raw_excerpt);
}

remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter(\'get_the_excerpt\', \'pietergoosen_custom_wp_trim_excerpt\'); 

结束

相关推荐

用于IE8的html5shiv的Genesis加载

Genesis Framework 2内置了对加载html5shiv的支持。js,使用genesis\\uhtml5\\uie\\ufix函数(/lib/js/load scripts.php)我将函数发布在下面。它应该在文档的头部打印一条条件注释,目标是IE 8及更低版本用任何其他浏览器加载网站时,注释存在于文档的头部。当我尝试用IE8加载它时,整个额外的块都消失了,因此IE8无法理解HTML5标记。如果我删除了条件注释,只保留了脚本标记,那么它可以正常工作,但额外的脚本正在为所有浏览器下载。看起来IE