Limit The Excerpt’s Word Count

 In Themes

Limit The Excerpt’s Word Count

One thing that can go wrong in WordPress magazine themes is when users include too many words before the more tag. Sure, they could handcraft the excerpt in the dedicated field, but on a website that has hundreds of posts and on which the text above the more tag has always been used as the excerpt, going back to create excerpts for all of those posts by hand would be cumbersome. In this case, we can limit the number of words shown in the excerpt by using this code:

add_filter('excerpt_length', 'ns_excerpt_length');
function ns_excerpt_length( $length ){
 return 10;
}

Here, we’re using a WordPress filter hook, which is a function that parses and (usually) modifies data before it gets stored in the database or displayed on a page. In this case, we’re setting the number of words shown in the excerpt to 10.

Recommended Posts

Leave a Comment

Start typing and press Enter to search