定制的get_the_excerpt()仅在第一次发布时有效

时间:2018-10-09 作者:op_garfield

我使用自定义get_the_excerpt 功能在我的主题中。它对循环中的第一篇文章非常有效,但其他文章都使用了标准的摘录功能。我不明白为什么?

海关get_the_excerpt 功能:

function wpse_allowedtags() {
    // Add custom tags to this string
    return \'<br>,<b>,<strong>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<h2>,<h3>,<h4>,<h5>\'; 
}

remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter(\'get_the_excerpt\', function($wpse_excerpt) {
    $raw_excerpt = $wpse_excerpt;

    if ( \'\' == $wpse_excerpt ) {
        $wpse_excerpt = get_the_content(\'\');
        $wpse_excerpt = strip_shortcodes( $wpse_excerpt );
        $wpse_excerpt = apply_filters(\'the_content\', $wpse_excerpt);
        $wpse_excerpt = str_replace(\']]>\', \']]&gt;\', $wpse_excerpt);
        $wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); 

        $excerpt_word_count = 150;
        $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\', $wpse_excerpt, $tokens);

        foreach ($tokens[0] as $token) { 
            if ($count >= $excerpt_length && 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;
        }

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

        $excerpt_end = \'&nbsp;…&nbsp;<a href="\'. esc_url( get_permalink() ) . \'">\' 
                . __(\'[ weiterlesen ]\', \'jankosyk\') . \'</a>\'; 
        $excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end); 

        $pos = strrpos($wpse_excerpt, \'</\');
        if ($pos !== false) {
            // Inside last HTML tag
            $wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); // Add read more next to last word 
        } else {
            // After the content
            $wpse_excerpt .= $excerpt_more; // Add read more in new paragraph 
        }

        return $wpse_excerpt;   

    }
    return apply_filters(\'wpse_custom_wp_trim_excerpt\', $wpse_excerpt, $raw_excerpt);
});
循环:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="entry"><?php
        if(is_single() || is_page()) {
            ?><h2><?php the_title(); ?></h2><?php
            the_content();
        } else { ?>
            <h2><a href="<?php the_permalink() ?>"><?php
                the_title(); 
            ?></a></h2><?php
            print get_the_excerpt();
        }  
    ?></div><?php
endwhile; ?>
该网站可在以下位置找到:https://jankosyk.de/

有什么建议吗?

谢谢

<小时>

Update:

由于我不使用post edit屏幕中的摘录,只想缩短帖子文本,因此我使用以下函数完全忽略了自动生成的每个摘录:

function wpse_allowedtags() {
    // Add custom tags to this string
    return \'<br>,<b>,<strong>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<h2>,<h3>,<h4>,<h5>\'; 
}
remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter(\'get_the_excerpt\', function() {
    $wpse_excerpt = get_the_content(\'\');
    $wpse_excerpt = strip_shortcodes( $wpse_excerpt );
    $wpse_excerpt = apply_filters(\'the_content\', $wpse_excerpt);
    $wpse_excerpt = str_replace(\']]>\', \']]&gt;\', $wpse_excerpt);
    $wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); 

    $excerpt_word_count = 150;
    $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\', $wpse_excerpt, $tokens);

    foreach ($tokens[0] as $token) { 
        if ($count >= $excerpt_length && 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;
    }

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

    $excerpt_end = \'&nbsp;…&nbsp;<a href="\'. esc_url( get_permalink() ) . \'">\' 
            . __(\'[ weiterlesen ]\', \'jankosyk\') . \'</a>\'; 
    $excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end); 

    $pos = strrpos($wpse_excerpt, \'</\');
    if ($pos !== false) {
        // Inside last HTML tag
        $wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); // Add read more next to last word 
    } else {
        // After the content
        $wpse_excerpt .= $excerpt_more; // Add read more in new paragraph 
    }

    return $wpse_excerpt;   
});

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

一种可能是,只需编写自定义过滤器,使其运行时间晚于默认优先级“10”(这就是“wp\\u trim\\u extract”函数的运行时间)。

get\\u The\\u摘录过滤器传递摘录和post对象。因此,您应该能够以更高的优先级运行过滤器,并使用post对象中的摘录值,而不是传递的摘录,因为这已经通过了另一个过滤器。

我将原来的匿名函数改为匿名函数,只是为了明确优先级和参数值。

// Running this filter at priority 20 so it comes later than wp_trim_excerpt()
add_filter( \'get_the_excerpt\', \'my_later_excerpt\', 20, 2 );
function my_later_excerpt( $possibly_filtered_excerpt , $post_obj ) {

    /*
     * Rather than change every instance of $wpse_excerpt in your
     * function, I changed the argument to $possibly_filtered_excerpt
     * and ignored it. Then set the $wpse_excerpt value to the 
     * excerpt contained in the post object ($post_obj->post_excerpt)
     */
    $wpse_excerpt = $post_obj->post_excerpt;

    $raw_excerpt = $wpse_excerpt;

    if ( \'\' == $wpse_excerpt ) {
        $wpse_excerpt = get_the_content(\'\');
        $wpse_excerpt = strip_shortcodes( $wpse_excerpt );
        $wpse_excerpt = apply_filters(\'the_content\', $wpse_excerpt);
        $wpse_excerpt = str_replace(\']]>\', \']]&gt;\', $wpse_excerpt);
        $wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); 

        $excerpt_word_count = 150;
        $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\', $wpse_excerpt, $tokens);

        foreach ($tokens[0] as $token) { 
            if ($count >= $excerpt_length && 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;
        }

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

        $excerpt_end = \'&nbsp;…&nbsp;<a href="\'. esc_url( get_permalink() ) . \'">\' 
                . __(\'[ weiterlesen ]\', \'jankosyk\') . \'</a>\'; 
        $excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end); 

        $pos = strrpos($wpse_excerpt, \'</\');
        if ($pos !== false) {
            // Inside last HTML tag
            $wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); // Add read more next to last word 
        } else {
            // After the content
            $wpse_excerpt .= $excerpt_more; // Add read more in new paragraph 
        }

        return $wpse_excerpt;   

    }
    return apply_filters(\'wpse_custom_wp_trim_excerpt\', $wpse_excerpt, $raw_excerpt);
}

结束

相关推荐

Increase offset while looping

我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组4个岗位。是否可以编写一个偏移量随每次循环而增加的查询?因此,结果将是:-第一个查询显示从1到4的帖子-第二个查询显示从5到8的帖子-第三个查询显示从9到12的帖子等。 <div class=\"official-matters-tabs\"> <?php $args = array(\'post_type\' => \'official-matters\', \'showp