The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Add a Custom Excerpt Length Based on Post Type in WordPress

Shared by Amit Dholiya · October 19, 2026

10 views
Back to Snippets

Steps

  1. Add the PHP code to your child theme’s functions.php file or the Code Snippets plugin.

  2. Save and activate the snippet.

  3. Check your website’s post excerpts.

Hook Used

– excerpt_length — Controls the number of words in generated excerpts.

Type: WordPress Content Customization

Output: The snippet sets the excerpt length to 30 words on single post pages and 20 words elsewhere when WordPress generates excerpts.

Customize: Change 30 and 20 to your preferred word counts.

Note: Some themes generate excerpts differently or display full post content, so this filter may not affect every template.

function custom_excerpt_length_by_post_type( $length ) {
    if ( is_admin() ) {
        return $length;
    }

    if ( is_singular( 'post' ) ) {
        return 30;
    }

    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length_by_post_type', 999 );

Tested on

WordPress 6.8

Reported by the author on October 19, 2026. WPFolks has not verified this.

Not tested against WordPress 7.1.4. It may still work.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by