This guide will show you how to change excerpt length in WordPress. This is helpful when the theme you use has an expert and is just too long or too short. Using the logic below we will change the except length using a WordPress function and filter. This allows you to quickly change the length to better flow with your overall site design or highlight a key amount of content for each post.
Why Change Excerpt Length in WordPress?
- You have the opportunity to optimize the teaser content to captivate your audience’s attention. A concise and compelling excerpt can create curiosity, encouraging visitors to dive deeper into your articles and engage with your website’s content.
- Improved User Experience: Long, unformatted excerpts can make it challenging for users to scan through your website’s content quickly. Shorter excerpts allow visitors to skim through multiple posts efficiently, gaining a quick understanding of the content, and deciding which articles to read in detail.
- WordPress themes often include templates that display post excerpts in various parts of your website, such as blog archives, category pages, or homepage displays. By adjusting the post excerpt length, you can ensure consistency in the design and layout of these sections. Consistent excerpt lengths contribute to a visually pleasing aesthetic, creating a cohesive look and feel across your website.
WordPress Default Excerpt Length
By default, an excerpt will contain up to 55 words. We can see what this looks like in my development version of my theme below.
Change Excerpt Length WordPress Function
To change the excerpt length we use the excerpt_length hook. This hook will fire before a post excerpt is retrieved so you can change the length. We add the code below to our themes
* Limit excerpt length
* Change the default returned length of post excerpts
* @link https://digi-dank.com/change-excerpt-length-in-wordpress/
* @link `excerpt_length` filter https://developer.wordpress.org/reference/hooks/excerpt_length/
* @param Int $length The maximum number of words. Default 55.
* @return Int The new number of words for excerpt length
add_filter('excerpt_length', 'digi_change_excerpt_length', 999);
When the hook is reached, we get a chance to apply our filter by using the add_filter function. If you are not familiar with how the filters work, I highly suggest taking some time to research further. As a WordPress developer this is critical to WordPress and theme customization.
In the example above I have the function return
25. This will change the new excerpt length to a maximum of 25 words. Once you have saved the code, you can refresh your archive page (or another page showing excerpts) and see the new results. We can now see the excerpts are approximately one line and much shorted as compared to before.
Change Excerpt Length in WordPress Wrap Up
Customizing the post excerpt length in WordPress offers great flexibility for improving user experience, maintaining design consistency, and even optimizing for SEO. As you can see changing expert length in php was very straightforward. As long as you have access to your theme code and can modify your
function.php you can make this change without requiring a plugin.
Now that you have seen how to change excerpt length in WordPress, how do you plan to use it on your theme? Drop a comment down below to get the conversation started!
If you would like to suggest a new article or video to be released, please reach out via the Contact Page. I look forward to hearing your feedback and providing content that is helpful for you!