Images with excerpt function

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

我有一个很好的摘录功能,可以让我的段落保持得体。顺便说一句,效果很好。

 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(\']]>\', \']]>\', $text);

    //Add the allowed HTML tags separated by a comma.
    $allowed_tags = \'<p>,<a>,<em>,<strong>\';  
    $text = strip_tags($text, $allowed_tags);

    //Change the excerpt word count.
    $excerpt_word_count = 105; 
    $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\');
为了显示包含摘录的第一幅图像,我使用了旧函数

    function pietergoosen_get_first_image() {
    global $post, $posts;
$first_img = \'\';
if(preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\', $post->post_content, $matches)){
$first_img = $matches [1] [0];
} 
    return $first_img;
}
这也很有效。

现在的问题是(我根本不是一个编码员),是否有办法和如何更改我提供的摘录代码,以自动显示第一幅图像,而不需要更多功能。任何代码都将受到高度赞赏

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

对于那些正在寻找不使用第一个图像功能而保持图像等老练的摘录的人,下面是代码

    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\'); 

结束

相关推荐

Bold letters inside excerpt

我目前正在尝试将摘录中的一些单词用“h1”标记加粗,但它似乎不起作用。当我在网上查看时,我所听到的只是建议使用插件。有没有一种方法可以在没有插件的情况下实现这一点。我的CSS如下所示。.side-bar h1 { font-weight: bold!important; }