首页 > 解决方案 > 以特定帖子类型的完整句子结束摘录

问题描述

对于我网站上的特定帖子类型,我试图使摘录以句子结尾,但由于某种原因,它也会影响页面摘录,我不明白为什么。

function vhr_variable_length_excerpt($text, $w_length, $finish_sentence){
    global $post;
    if ( $post->post_type == 'poi' ) {
        //Word length of the excerpt. This is exact or NOT depending on your '$finish_sentence' variable.
        $w_length = 20; /* Change the Length of the excerpt. The Length is in words. */
     
        //1 if you want to finish the sentence of the excerpt (No weird cuts).
        $finish_sentence = 1; // Put 0 if you do NOT want to finish the sentence.
            
        $tokens = array();
        $out = '';
        $word = 0;

        //Divide the string into tokens; HTML tags, or words, followed by any whitespace.
        $regex = '/(<[^>]+>|[^<>\s]+)\s*/u';
        preg_match_all($regex, $text, $tokens);
        foreach ($tokens[0] as $t){ 
            //Parse each token
            if ($word >= $w_length && !$finish_sentence){ 
                //Limit reached
                break;
            }
            if ($t[0] != '<'){ 
                //Token is not a tag. 
                //Regular expression that checks for the end of the sentence: '.', '?' or '!'
                $regex1 = '/[\?\.\!]\s*$/uS';
                if ($word >= $w_length && $finish_sentence && preg_match($regex1, $t) == 1){ 
                    //Limit reached, continue until ? . or ! occur to reach the end of the sentence.
                    $out .= trim($t);
                    break;
                }   
                $word++;
            }
         //Append what's left of the token.
         $out .= $t;     
        }
        return trim(force_balance_tags($out)); 
    }
}

function vhr_excerpt_filter($text){
    global $post;
    if ( $post->post_type == 'poi' ) {
        //Get the full content and filter it.
        $text = get_the_content('');
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
    
        $text = str_replace(']]>', ']]&gt;', $text);
    
        //If you want to Allow SOME tags: 
        $allowed_tags = '<p>,<a>,<strong>,<b>'; /* Here I am allowing p, a, strong tags. Separate tags by comma. */
            
        $text = strip_tags($text, $allowed_tags);
    
        //Create the excerpt.
        $text = vhr_variable_length_excerpt($text, $w_length, $finish_sentence); 
  
        return $text;
    }
}
//Hooks the 'vhr_excerpt_filter' function to a specific (get_the_excerpt) filter action.
add_filter('get_the_excerpt', 'vhr_excerpt_filter', 5);

它不会影响我的任何其他自定义帖子类型,只会影响我在函数中指定的类型,然后是网站上的所有页面。即使我将逻辑更改为 = poi && != page 之类的东西,它仍然会影响我的页面。任何想法为什么这也会影响页面?有没有更简单的方法来实现这一点?

谢谢!

标签: phpwordpress

解决方案


我们有很多方法可以接近它。花时间编写自定义摘录而不是依赖 Wordpress 就是其中之一......

我们可以通过瞄准句末期来计算句子。

function get_sentence_tally_excerpt( $content = '', $tally = 2, $stitches = '' ) {

    $buffer = array_slice( explode( '.', sanitize_text_field( $content ) ), 0, $tally );

    $filter = array_filter( array_map( 'trim', $buffer ), 'strlen' );

    $excerpt = join( '. ', $filter ) . '.';

    return esc_attr( $excerpt . $stitches );

};

您可以指定应该“截断”什么类型的内容以及多少个句子。在前端,我们可以get_tally_excerpt()这样调用我们的函数:

<?= get_sentence_tally_excerpt( get_the_content() ); ?> //... 2 sentences by DEFAULT 
<?= get_sentence_tally_excerpt( get_the_content(), 1 ); ?> //... 1 sentences ONLY
<?= get_sentence_tally_excerpt( get_the_content(), 5, '[...]' ); ?> //... 5 sentences ONLY with stitches at the end. 

推荐阅读