Limit WordPress Excerpt by Word Count

Rather than using normal WordPress default excerpt function, which we specified how many words will appear according to what we have typed, we can limit the_excerpt() or get_the_excerpt() by a specific word count for all posts.

Here’s a nice little bit of code that can come in very handy. Simply add these code snippets into your functions.php.

[sourcecode language=’php’]

function my_excerpt_length($text){
return 20;
}
add_filter(‘excerpt_length’, ‘my_excerpt_length’);

function new_excerpt_more($more) {
return ‘…’;
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);

[/sourcecode]

  • One rule to use this function is that in your current template must be using the_content() funtion to call post content, otherwise this will not work.

This will show at least twenty words of your excerpt if you have more. Try it!