Back to Top

How to set word limit on the_content() in WordPress

Limit by words 'the_content' in WordPress

Sometimes, you can not limit the content by character or letter.It distracts the layout of content so you want to set word limit on the_content() in WordPress so content displaying the site looks pretty good.Well, In WordPress, we are using a the_content function to displaying the content of post or blog.So here we need to customize the_content function which we can do by applying the hook into it.Open theme’s file functions.php and place the following code into it.

You just need to copy and paste the above code and just change the $word_limit variable value to the desired limit you want.

Now, you are finished.If you have any query or any confusion then feel free to comment me.

Hope this post will helpful for you, waiting for your responses.Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (4)

  1. It works but does not work. It also effects when you click on the post (in single.php).

    Also, I want to ask how to add … or read more using this snippet.

  2. This one works:

    add_action(‘the_content’,’limit_the_content’);
    function limit_the_content($content){
    $word_limit =90;
    $words = explode(‘ ‘, $content);
    if (!is_single()) {
    return implode(‘ ‘, array_slice($words, 0, $word_limit)).” …”;
    } else {
    return $content;
    }
    }

  3. Antonio Rodriguez Olmos says:

    Thank you Lal! Yours is the only method that works đŸ˜€

  4. Code is good and working but the issue is words limitation applied with all post pages like index.php and single.php etc.
    I have done some modification and applied if else condition to show full content in single.php

    add_action(‘the_content’,’limit_the_content’);
    function limit_the_content($content){
    if(!is_single())
    {
    $word_limit =40;
    $words = explode(‘ ‘, $content);
    return implode(‘ ‘, array_slice($words, 0, $word_limit));
    }
    else
    {
    $word_limit =10000;
    $words = explode(‘ ‘, $content);
    return implode(‘ ‘, array_slice($words, 0, $word_limit));
    }
    }

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to Create a Custom WordPress Widget

Posted on 11 years ago

Bhumi