Change the WordPress Default Excerpt Length
June 5, 2020 • PHP, WordPress • Published By Josh • 2 minute read
A WordPress post can have an excerpt that you define manually. It can be used to display a summary or teaser for your post. If you prefer the excerpt get automatically generated for you, it has a default length of 55 words. If that length is not the right size for your needs, you can easily change it!
Table of Contents
Manual Excerpt
An excerpt can be defined manually by using the Excerpt section in your editor. What you define here is what will display as the post excerpt when you call the_excerpt() in your template.
Automatically Generated Excerpt
If you prefer that WordPress automatically generate an excerpt for you, then leave the Excerpt section in your editor empty. As previously stated, the default length of an automatically generated excerpt is 55 words. If you wish to change that length, go to your theme’s functions.php file by navigating to Appearance > Theme Editor.
Change the default length
In your theme’s functions.php file, you can simply add the following:
function custom_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'custom_excerpt_length' );
Reviewing the Code
WordPress provides a filter called excerpt_length which gets called at runtime. Define a callback function as the second argument so that when the filter is run, it will execute the function. In this example, custom_excerpt_length is the name of the callback function.
add_filter( 'excerpt_length', 'custom_excerpt_length' );
Return a number that represents the number of words the excerpt length should be!
function custom_excerpt_length( $length ) {
return 30;
}