How to Customize WordPress Excerpt Length
Learn how to customize automatically generated WordPress excerpts using PHP. Change the default 55-word limit to 30 words and replace the excerpt ending without installing an extra plugin.
Updated

/**
* Change the length of automatically generated excerpts.
*
* @param int $length Default excerpt length.
* @return int
*/
function wdg_custom_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'wdg_custom_excerpt_length', 999 );
/**
* Replace the default excerpt ending with an ellipsis.
*
* @param string $more Default excerpt ending.
* @return string
*/
function wdg_custom_excerpt_more( $more ) {
return ' …';
}
add_filter( 'excerpt_more', 'wdg_custom_excerpt_more' );
Introduction
WordPress automatically generates short summaries (excerpts) from your posts when no manual excerpt has been provided. The default length is typically 55 words.
In this snippet, you’ll change that limit to 30 words and replace the default […] ending with a clean ellipsis—all without an extra plugin.
Difficulty: Beginner
Technology: WordPress, PHP
Code language: PHP
What You’ll Build
A small, reusable PHP snippet that changes the length and ending of automatically generated WordPress excerpts.
The PHP Snippet
Use the Primary Code example displayed above this article. Add it to your child theme’s functions.php file or a small custom plugin, as explained below. The full PHP code is stored once in the Snippet’s Primary Code field, rather than duplicated in the article body.
Customize it: Change 30 to your preferred word limit, such as 20, 40, or 60.
Where to Add the Code
Option 1: Child Theme
- Open
wp-content/themes/your-child-theme/functions.php. - Paste the snippet into the existing PHP code.
- Save and reload a blog archive or another page that displays excerpts.
Do not add a second <?php opening tag to an existing PHP file. Avoid modifying your parent theme because updates can overwrite your changes.
Option 2: Custom Plugin
Place the same functions in a small custom plugin to keep the behavior independent of your theme. You can also use a PHP code-snippet plugin; follow its instructions about including or omitting the opening PHP tag.
How It Works
1. excerpt_length
WordPress applies the excerpt_length filter while generating a summary from post content. The function returns 30 instead of the usual 55-word limit. Priority 999 runs after many other callbacks, although another callback can still override it.
2. excerpt_more
WordPress normally adds an ending similar to […] when it shortens an automatic excerpt. Our second function replaces that ending with …, including a leading space to separate it from the final word.
3. Displaying the Excerpt
Classic WordPress templates commonly display excerpts using:
<?php the_excerpt(); ?>
If your theme already uses the_excerpt(), no template changes are needed.
Example Result
Before: An automatically generated excerpt may show 55 words followed by […].
After: That excerpt shows up to 30 words followed by ….
The ending appears only when text is actually shortened.
Important Notes
- Manual excerpts: The
excerpt_lengthfilter does not shorten text manually entered in the post’s Excerpt field. - Gutenberg Post Excerpt block: The Post Excerpt block has its own Max number of words setting. In a Query Loop or block theme, adjust this setting rather than expecting the global PHP filter to control the block.
- Theme or page builder logic: Some themes and builders generate their own excerpts and may not use the standard filter.
- Original content: This snippet never modifies saved post content; it only affects excerpt generation.
Troubleshooting
The excerpt still has 55 words. Check whether the page uses the Gutenberg Post Excerpt block, a manually written excerpt, or a custom theme function.
The change is not visible. Clear any site or page cache and reload the archive.
There is no ellipsis. Your post may be shorter than the chosen excerpt length.
Conclusion
The excerpt_length and excerpt_more filters make it easy to control standard WordPress-generated summaries. Two small PHP functions give your blog cards cleaner, more consistent excerpts.