Trim Your Text with wp_trim_words() in WordPress
by c.bavota | Posted in Tutorials | 26 comments
May 14 2012
When I first started putting out Premium themes for WordPress, I create a custom excerpt function that would trim your post content to a certain number of words. Today, I discovered that WordPress 3.3 actually has a similar core function called wp_trim_words().
The function works like so:
<?php wp_trim_words( $text, $num_words = 55, $more = null ); ?>
The parameter break down:
- $text
- (string) (required) Text to trim
Default: None - $num_words
- (integer) (optional) Number of words
Default: 55 - $more
- (string) (optional) What to append if $text needs to be trimmed.
Default: ‘…’
If you wanted to display a trimmed version of your content you could do it like so:
<?php echo wp_trim_words( get_the_content(), 100 ); ?>
Using the above snippet within the WP loop would display your content, trimming it at 100 words and adding the default ‘…’ afterwards.
It’s a great function that allows you to automatically trim certain text elements within your WordPress theme.



So is this a replacement for the_excerpt(), using words rather than characters? What exactly happens when I click “read more”?
Actually that read more that I included was not a link. I switched to an ellipses. Gonna test if I can pass an actual link to that third parameter.
Ah, ok, so you have to them add a permalink after your ellipsis. I get it!
this function just return exactly the number of word and nothing more
I think this is similar in function to the excerpt() which limits the post to 55 words. For the wp_trim_words() you can freely decide for yourself whatever limit you would like to have.
Can you use this script with meta values ?
How so? The script will trim any text to a certain number of words.
Oh sorry I misread the parameter “$text”. I thought it was only for the excerpt and content.
Looks like a cleaner way. I’ll try it.
Sorry I meant to subscribe to the comments so I’m posting again…
Awesome! Thanks for the tip
Oh ya…don’t forget the echo
Thanks.
Thank you so much for the information. I was searching for how to add custom fields to WP author page and came across an article you wrote. While it didn’t work out for me, still thanks for posting WordPress tricks.
Hay, I noticed you are from Canada too! LOL I am from Calgary and nice to see more Canadians on the net!
This Trim is so ql
Nice function, thanks for sharing!
good job…thanks for shared.
Thanks a lot! You saved my day.
Awesome trick! I tried this like a year ago but I could not make it work. Your info made it work. Thanks.
what’s awesome? this is not magic, rather say how to leave formatting when using this function….after using is content destroyed
WordPress strips all formatting from the excerpt, unless you use the actual Excerpt box on your post edit admin screen.
I searched for how to do this for hours. Thanks for finally answering my questions.
This is a great way to trim the code thanks. It is very clean as well
How is this better than inserting the “Read More” directly within the post? At least that way you can control EXACTLY where the text is cut off. I think that would make for a cleaner website, but of course that is just my opinion.
This was exactly what I needed!
I’m all done now fixing my blog and it looks awesome!
Thanks for this final touch