用第一句话截取摘录

时间:2017-11-18 作者:Rada Wilinofsky

我想用第一句话来结束我的摘录。我尝试了在这个网站上找到的解决方案,包括each 函数+从以下帖子添加过滤器:

How to end the excerpt with a sentence rather than a word?

End excerpt at the end of the sentence

它没有起作用。我已通过添加以下函数检查是否应用了过滤器:

function print_filters_for( $hook = \'\' ) {
global $wp_filter;
if( empty( $hook ) || !isset( $wp_filter[$hook] ) )
    return;

print \'<pre>\';
print_r( $wp_filter[$hook] );
print \'</pre>\';
}
并在模板上调用它。它出现了。我曾尝试通过添加$priority参数来切换它与通用wp\\U trim\\u摘录的应用顺序,但这并没有什么区别:摘录仍然会按字数进行修剪。如果有区别的话,我正在使用Divi并制作一个儿童主题。

有人知道问题出在哪里吗?

1 个回复
SO网友:Jim-miraidev

试试@Pieter Goosen的解决方案,我发现这是最好的解决方案,而且很容易定制。

if ( ! function_exists( \'pietergoosen_custom_wp_trim_excerpt\' ) ) : 

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

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

            //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\', $pietergoosen_excerpt, $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;
                }

            $pietergoosen_excerpt = 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($pietergoosen_excerpt, \'</\');
                //if ($pos !== false)
                // Inside last HTML tag
                //$pietergoosen_excerpt = substr_replace($pietergoosen_excerpt, $excerpt_end, $pos, 0);
                //else
                // After the content
                $pietergoosen_excerpt .= $excerpt_end;

            return $pietergoosen_excerpt;   

        }
        return apply_filters(\'pietergoosen_custom_wp_trim_excerpt\', $pietergoosen_excerpt, $raw_excerpt);
    }

endif; 

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

结束