How to Change the Default Excerpt Length In WordPress
One of the main reasons to use WordPress for your site is because a lot of your content is automated by your theme and by WordPress. One of these advantages is the excerpt function which by default displays the first 55 words of your post on archives (categories, tags, homepage, search, etc).
But what if you wanted to show more or less then the default 55 words for your post excerpts? In this guide I’ll show you how you can modify your excerpt length and provide a little advice on how I deal with excerpts in WordPress.
Using the WordPress “More” Block for Excerpts
One thing you can do is use the built-in “more” block when editing your posts which allows you to specify the exact location for your excerpt or use the excerpt field which allows you to paste a custom excerpt for your posts.
The issue that I have with using the more block is that it creates inconsistency on your site and it can look really bad depending on your site design to have entries with different excerpt lengths. I think auto generated excerpts are best to ensure all of your entries match. This is especially important if your theme uses a grid based display for your entries so they display side by side.
Now, in terms of SEO though I think using the custom excerpt field is ideal because you can give your posts unique excerpts and prevent possible duplicate content issues on the site. What I’ve done in the past was simply copy my Yoast SEO meta description into my excerpt field so that they match (this could also be done with some custom code though so it’s automatic).
More Block Not Working? It’s possible that your theme doesn’t natively support the more block (also known as the more link in the classic editor). In which case you should reach out to the theme developer for support since this is a native WP function that should be supported by all themes.
Changing the Excerpt Length For All Loops
Using the more block or custom excerpt field for every single post can be a real pain but also as I mentioned above it can create inconsistencies. Luckily with a little code you can easily change the default excerpt length in WordPress.
WordPress has a built-in filter appropriately named excerpt_length
that will allow you to change the default length of your excerpts. Simply insert the following piece of code into your child theme at the bottom of your functions.php
file and edit the 20
to the number of words you want for your excerpts.
// Change the default excerpt length to 20 words.
function wpexplorer_excerpt_length( $length ) {
$length = 20;
return $length;
}
add_filter( 'excerpt_length', 'wpexplorer_excerpt_length', PHP_INT_MAX );
Notice I am using PHP_INT_MAX for the hook priority to ensure the code runs last and overrides your theme or any possible plugin that may also be altering excerpt length.
It’s very important to note that this code will only work if your theme is actually using the core get_the_excerpt
or the_excerpt
functions. If your theme is using it’s own custom excerpt function then this snippet will not work. For example our Total theme includes built-in settings that allow you to modify the excerpt length for every grid you insert on the site as well as independently for any archive (blog, portfolio, staff, search, testimonials, search, etc).
Different Excerpt Length by Post Type
You may want to use different excerpt lengths based on the current post type. For example you may want to display larger excerpts for your blog posts and smaller excerpts for your staff members. You can easily set different excerpts for each post type on your site by simply checking the current post type and returning a different length, example:
// Change the default excerpt length to 20 words.
function wpexplorer_excerpt_length( $length ) {
$post_type = get_post_type();
switch ( $post_type ) {
case 'staff':
$length = 20;
break;
case 'portfolio':
$length = 40;
break;
case 'post':
default:
$length = 60;
break;
}
return $length;
}
add_filter( 'excerpt_length', 'wpexplorer_excerpt_length', PHP_INT_MAX );
This is a basic example showing how you may set different excerpts based on the post type being displayed, however you can use any other conditional for example if you wanted to display a different excerpt for categories and tags or for the search results. It doesn’t make sense for me to share every possible snippet here, but if you are having trouble writing your own code let me know in the comments so I can assist.
Advanced Custom Excerpt Function
For developers looking to create custom excerpts for your theme or plugin I’ve written a tutorial where I share a custom function you can paste into your project. This advanced excerpt function will allow you to easily display different excerpt lengths across the site and give you full control over the excerpt rather then using the core WP functions.
Simply visit the How to Add Multiple Custom Excerpt Lengths In WordPress tutorial to learn more and check out the code. And as always feel free to use any code snippets on our site for your free or premium products without providing any credit, of course if you want to stop by and say thank you in the comments I do really appreciate that!
This is exactly what I was looking for… Thanks!
This post is very useful, thanks a lot!
Thanks, it worked!
That’s great to hear!
In a loop it only works for the last post… any ideas?
It should work for all your posts because the filter runs everywhere. If it’s not then it sounds like your theme may not be using the core WordPress get_excerpt() function or perhaps your posts actually have custom excerpts inserted into the page either via the excerpt field or the “more” link. WordPress will only trim your excerpt if it’s being automatically generated by WordPress.