Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Excerpt Length Won’t Shorten

  • Hi, I have added an excerpt to the excerpt section of a post. I have tried shortening the length a couple of ways (and purged the cache) but neither of them seem to work.

    1 – Appearance > Customize > Layout > Blog > Changed to 20 (It didn’t work)
    2 – Within the (Magazine Latest Posts) Element query loop template. Data Source (Current Post), Content Source (Excerpt), Excerpt Length (20). Still didn’t work.

    Can you help please?
    (page in the private area)

  • Hi there,

    Try this PHP snippet:

    add_filter( 'wp_trim_excerpt', 'yh_trim_custom_excerpt' );
    
    function yh_trim_custom_excerpt( $excerpt ) {
        $output = $excerpt;
    
        // Limit word count to 20
        $words_limit = 20;
    	
        $excerpt_words = preg_split('/[\s]+/', $excerpt, $words_limit + 1);
        if (count($excerpt_words) > $words_limit) {
            array_pop($excerpt_words);
            $excerpt = implode(' ', $excerpt_words);
        }
    
        if ( has_excerpt() ) {
            $output = sprintf( '%1$s... <a href="%2$s">%3$s</a>',
                $excerpt,
                get_permalink(),
                __( 'Read more', 'generatepress' )
            );
        }
    
        return $output;
    }
  • Thanks Ying, that’s perfect.

  • You are welcome   🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.