Written by: John | 22nd Jun 2015
If you have been using WordPress on multiple projects you must have come up against needing to pull a custom excerpt size?
Now there are many ways to do this, eg, you could use this to pull say 80 characters of copy:
//get the excerpt $excerpt = get_the_excerpt(); //strip the html $excerpt = strip_tags ($excerpt); //return 80 chars $excerpt = substr($excerpt, 0, 80);
But I found that WordPress already has a function built in to return a defined number of WORDS not CHARS. Now this is achievable using php string manipulation, but if there is a function already existing, why re-invent the wheel?
Here it is:
$excerpt = get_the_excerpt(); //simply return the first 10 words. $excerpt = wp_trim_words( $excerpt, $num_words = 10, $more = '… ' );
How easy is that?