How to Limit WordPress the_content Words Length?

If you are a WordPress theme developer, you know that the_content function displays all the words whenever you use it. In this short tips and trick tutorial, you will learn how to limit WordPress the_content words length . We will show you the different ways to do that plus the best and recommended way.

How to Limit WordPress the_content Words

You use the_content function in almost every page of your WordPress theme. For instance you use in index.php file and  it will echo all the content for one post or page. But you don’t want to show all content. Maybe only 50 words. Here how you can limit the_content.

Using Filter Hoock

Open your functions.php file and write the following code.

 

The only problem with this method is that, it will filter the_content and shows 500 character no-matter where you have used. For example, if you have used in in single post which you display all the content, it will display only that 500 characters. ( not a good way )

Using wp_trim_words

This method is probably easy and short solution. Every time you use the_content function, apply the wp_trim_words function on it.

You can’t use the_content function because you it will echo the content directly. Instead, we use get_the_content() function. 

Using Custom Function

This method is going to be the best and most flexible way to limit WordPress the_content words length. Write the following custom function in your functions.php file.

Use the custom function instead of the_content. For example:

Conclusion

You can use the_excerpt() function as will to display less content. But still you will need to modify the length of the words in that. We hope you have learn something and use it in you next project. Feel free to ask you question bellow. 🙂

<p>Web Designer and Developer. I work with HTML5, CSS3, JavaSript, PHP, WordPress, Laravel … etc.</p>

One Comment

  1. Thank you, this helped a great deal!

Leave a Reply