Steps
-
Add the PHP code to your child theme’s
functions.phpfile or the Code Snippets plugin. -
Save and activate the snippet.
-
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
Reported by the author on October 19, 2026. WPFolks has not verified this.
Not tested against WordPress 7.1.4. It may still work.
