Trim Your Text with wp_trim_words() in WordPress

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:


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:


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.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie