apply_filters( 'excerpt_length', int $number )
Filters the maximum number of words in a post excerpt.
Contents
Parameters
-
$number
int -
The maximum number of words. Default 55.
More Information
Use this filter if you want to change the default excerpt length.
To change excerpt length, add the following code to functions.php
file in your theme adjusting the “20” to match the number of words you wish to display in the excerpt:
function mytheme_custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'mytheme_custom_excerpt_length', 999 );
Make sure to set the priority correctly, such as 999, otherwise the default WordPress filter on this function will run last and override what you set here.
Source
File: wp-includes/formatting.php
.
View all references
$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
Changelog
Version | Description |
---|---|
2.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Set the excerpt length based on a theme mod, through the Customizer.
This will help to apply the excerpt_length on front end only.
Top ↑
Feedback
It’s important to set the priority to a high number so your callback is not overwritten by the WordPress default callback. — By leogermani —
Notes: This will display your excerpt text up to 30 words using excerpt_length filter. By default the number of words is 55.
Example:-
This will display your post excerpt up to 30 words.
This is useful to display different excerpt length on homepage, custom page template (where listing some custom post types).