Change The Excerpt Length Of Your WordPress Posts

This is fairly easy to do with the help of a filter called excerpt_length.

 * Filter the except length to 20 words.
 * @param int $length Excerpt length.
 * @return int (Maybe) modified excerpt length.
function my_custom_excerpt_length( $length ) {
    return 30;
add_filter( 'excerpt_length', 'my_custom_excerpt_length', 999 );

This piece of code can be added to the functions.php file of your theme OR it can be added to your site via a plugin called Code Snippets.

What does it do? In simple words, it creates a function that accepts the $length argument and returns it by setting the length to 30.

This function is hooked onto the filter or an event in WordPress called excerpt_length.

Whenever this event is fired, it will trigger the my_custom_excerpt_length function with a priority of 999. That’s all.

Leave a Reply

Your email address will not be published. Required fields are marked *