WP: Trim Words

Today was another busy day. I’m writing this post in the last hour of the day to maintain the blogging streak.

This will be short post about fetching the content on your WordPress site and trimming it to a set number of words.

We know that the_content() function gets the content in the famous WordPress loop, but it displays everything from start to end.

If we need to fetch the content and display a set number of words, it can be done by using the following two functions:

  • get_the_content()
  • wp_trim_words(content, words)

To be specific, they can be used as follows:

echo wp_trim_words(get_the_content(), 18)

This will get the content, trim it to 18 words and echo it out on the screen. That’d be all in this post. Thanks for reading 🙂