Wordpress: Custom Excerpt Length
Customize the length of post excerpts in your WordPress site with this easy-to-use snippet, perfect for theme developers and content managers.
Controlling the length of excerpts on your WordPress site can play a crucial role in how content is presented and consumed. This snippet provides a simple way to customize the length of excerpts based on specific needs.
The excerpt_length
filter is used to adjust the number of words in post excerpts. This is particularly useful for theme developers who need to fit excerpts into a specific design layout without overcrowding or truncating too much content.
This example sets the excerpt length to 20 words, which can be adjusted according to the design requirements or the nature of the content being displayed.
The snippet is easy to modify for different contexts or use cases, such as shorter excerpts for search results or longer excerpts for main blog pages.
Additionally, it includes tips on how to apply these changes conditionally, such as only for certain post types or within specific sections of the site.
Snippet Code
Version Compatibility
This code is effective with WordPress 2.9 and above, where the excerpt_length filter was introduced.
Required PHP Version
PHP 5.2 or higher
Wordpress Core Functions Used
Use Cases
- theme development
- content display
- user engagement