WordPress 3.3 brought a handy little function with it in wp_trim_words(). An example use case is a height limited box like the one pictured, where you want to make sure that the title and excerpt don’t cause the text to overflow the box.
Using wp_trim_words() allows us to limit the title and excerpt, and anything else really, to a particular number of words. How practical a name. You can define the number of words to limit them to, and also define what you’d like the “more” text to be. My example just uses ellipses.
Below is a gist to show it in use. Everything but the use of wp_trim_words() is totally arbitrary, but there for fun.
Now quit reading and go make some stuff.
20 thoughts on “Quick tip: Use wp_trim_words() to limit words in WordPress”
The function is expecting a value for the text that doesn’t return. That’s why you’d use get_the_excerpt() or get_the_title(). It’s your best bet to use functions that do not return and build your own HTML around the results of wp_trim_words() in a stored variable.
My friend u don’t know who easy you make it….maaaaaaaaN. …..I was really searching for this solution from recent two months… NOw i can use it simply and easyle……Thanks bro………….thanks
Hello there. Love this tip. Please can you help with any way to retain the formatting of the text that is called? It seems that it has to wipe out all formatting which is a pain 🙁
If you see my title above, you can see $shorttitle is saving the output of wp_trim_words(). You can use it just like I do where you use the variable it’s saved to in any html format you like. All in the code up there : )
Nice tip!. Would anyone know if it is possible to trim the text once the first full stop is reached…rather than just a random word after a particular number of words reached?
To my knowledge, you’d need custom PHP for that. But be careful, because you never know when a sentence will have a period like Mr. or Ms. or St. or something like that within the first sentence. Would be a tough thing to search for.
Nice catch, I haven’t used that one yet.
Doesn’t surprise me you haven’t used it yet. You’re just a blogger.
*chuckles*
LOL 🙂
What a great solution. Thanks for sharing your discovery Brian.
If you keep using $krogsquery, I wonder how long it will be before it ends up in a distributed theme someplace.
Haha, that’d be pretty funny : ) Thanks for the kind words, Bill.
Awesome tip. Thanks Brian!
I usually end up rolling some half-baked custom function to do that. Nice to see there’s something built into core for it now.
Score. Any idea if it preserves HTML?
hmm…
The function is expecting a value for the text that doesn’t return. That’s why you’d use
get_the_excerpt()
orget_the_title()
. It’s your best bet to use functions that do not return and build your own HTML around the results ofwp_trim_words()
in a stored variable.oh no….
now i have to update all my theme..
thanks alot brian…
you are awesome!
My friend u don’t know who easy you make it….maaaaaaaaN. …..I was really searching for this solution from recent two months… NOw i can use it simply and easyle……Thanks bro………….thanks
Hello there. Love this tip. Please can you help with any way to retain the formatting of the text that is called? It seems that it has to wipe out all formatting which is a pain 🙁
Any help greatly appreciated!
Hmm, I think you’d need to rebuild the html yourself as it expects the format from get_the_title();
Hi, any advice on that?
I am currently pulling in custom fields as follows:
echo wp_trim_words( get_field(‘field_name’), 1000, ‘ ‘ );
and wrapping in wp_trim_words. It works fine except that there is no formatting…which is no good.
Hope you can offer some advice.
Many thanks. AB
If you see my title above, you can see $shorttitle is saving the output of wp_trim_words(). You can use it just like I do where you use the variable it’s saved to in any html format you like. All in the code up there : )
Thanks. I just don’t understand how I can re-apply line breaks (the only issue I have) to a long section of text?
Nice tip!. Would anyone know if it is possible to trim the text once the first full stop is reached…rather than just a random word after a particular number of words reached?
To my knowledge, you’d need custom PHP for that. But be careful, because you never know when a sentence will have a period like Mr. or Ms. or St. or something like that within the first sentence. Would be a tough thing to search for.
Great Trix. Thanks for sharing.