How to Limit Content in WordPress

This is an old article describing old-fashioned ways of limiting content. I’m publishing a new article on this topic very soon so make sure you subscribe below.

Have you seen those cool magazine style WordPress themes around this days? They are just cool. You might have noticed that they sometimes show a few words of the post, this is for sure not the default WordPress excerpt showing that because the default excerpt length is set to 55 words and manually excerpting every post would be a great time consumer. So, what are this guys putting together to get that cool effect on their sites by controlling the length of the content? Well, as a WordPress freelancer, I have been needing that feature myself and today I’m going to share with you how to have that feature on your site.

There are three different approaches to limit your excerpt or content in WordPress that you must know:

1. WordPress’s Approach

WordPress developers have provided us lately with a tool to limit the excerpt. I do not use it myself as I’m more conformable with another approach as I find this a little limited. To change excerpt length using the default WordPress excerpt limit approach you have to use the excerpt_length filter and add the following code to functions.php file in your theme:

function new_excerpt_length($length) {
      return 20;
add_filter('excerpt_length', 'new_excerpt_length');

Every time you will use the_excerpt(); on your code it will output an 20-word text string for you. Right now you’ve got a question, don’t you? I know, I had it too. The question is: What if I want to use the 20-word excerpt in some places on my theme, but still be able to use the 55-word one? Or just have two or three different-length exerts to use on my theme? Well, if that is the case, the default approach is not for you, so continue reading.

2. The StudioPress Approach

I don’t really know who came up with this approach originally, but I know that the guys over at StudioPress use it on their themes. This function is called the_content_limit(). This is how the code that will go into the functions.php file looks like – lengthy huh?

function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') { 
$content = get_the_content($more_link_text, $stripteaser, $more_file); 
$content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); 
$content = strip_tags($content); 
if (strlen($_GET['p']) > 0) { 
echo ""; echo $content; echo " "."Read More →"; echo ""; 
else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) { 
$content = substr($content, 0, $espacio); $content = $content; echo ""; 
echo $content; 
echo "..."; 
echo "".$more_link_text."
"; echo ""; 
} else { 
echo ""; echo $content; echo " 
"."Read More →
"; echo ""; 
} }

Now we have to call that function because, off course, we are not going to use the default the_excerpt(); function. Here:

the_content_limit(20, "[Read more]");

Pretty simple to use and offers you the possibility to auto-generate a Read More … of you choice.

C.bavota’s Apprach

C.bacota – I don’t know his real name – has produced a couple of great code snippets to limit the content for both the_content() and the_excerpt(). As you may already know, the_excerpt() gets 55 words from the_content() but get rids of all the HTML tags on it, be it links, images or whatever. So, this guy produced a script that allows you to choose to use the text-only version or the html-rich version to limitedly display. I find this functions really cool and easy to use and I have been using them ever since they were produced. This is how the block of code that goes into functions.php looks like:

// Limit the content to certain number of words.
 function excerpt($num) {
 $limit = $num+1;
 $excerpt = explode(' ', get_the_excerpt(), $limit);
 $excerpt = implode(" ",$excerpt)."...";
 echo $excerpt;
 // Limit the content to certain number of words.
 function content($num) {
 $limit = $num+1;
 $content = explode(' ', get_the_content(), $limit);
 $content = implode(" ",$content)."...";
 echo $content;

The above code is quite cool, but has two limitation: The first block of code, will not allow you to have a custom excerpt longer than 55 words, the same as the_excerpt() does, and the second one will bump image into your shorten content if you are using images in the original one. However, Mr. C.Basoda eventually thought about his and produced later a revised version of the code that will allow you to use more than 55 words and not get images on the wrong place there. Here is the revised code:

function excerpt($limit) {
 $excerpt = explode(' ', get_the_excerpt(), $limit);
 if (count($excerpt)>=$limit) {
 $excerpt = implode(" ",$excerpt).'...';
 } else {
 $excerpt = implode(" ",$excerpt);
 $excerpt = preg_replace('`[[^]]*]`','',$excerpt);
 return $excerpt;
function content($limit) {
 $content = explode(' ', get_the_content(), $limit);
 if (count($content)>=$limit) {
 $content = implode(" ",$content).'...';
 } else {
 $content = implode(" ",$content);
 $content = preg_replace('/[.+]/','', $content);
 $content = apply_filters('the_content', $content);
 $content = str_replace(']]>', ']]>', $content);
 return $content;

And finally here is how you use this fantastic piece of code:


// or

Final Thoughts

We are finally done.  I would also like to make clear that the approaches I presented so far are not necessary all the existing or possible ways to achieve content limitation, however this is what I know and what I thought was of value to share with you. Thanks for reading.

Baki Goxhaj
I’m a web consultant specializing in WordPress. I love elegant solutions, quality code and a good laughter.
You may also like
PHPloy - PHP Git Deploy
PHPloy – Deploy Git Repos Easily through FTP/sFTP
WordPress Bug: Tags do not appear under WP_Query
  • Lukasz Drazewski May 1,2016 at 3:20 pm

    Thanks Man! This is what I need. And it works fine!

  • ne iyi gelir Dec 8,2015 at 11:29 pm

    I forgot to tell, the long code goes in functions.php and the $str and echo goes within your loop!

  • […] How to Limit Content in WordPress […]

  • Liam Kenneth Jul 3,2013 at 10:19 am

    Didn’t anyone notice the studio code doesn’t create a link? i just have ‘Read more’ in plain text and cant see a way to ad the post url in the href of a tag. (that’s also missing from the code)

    • Baki Goxhaj Jul 6,2013 at 7:19 pm

      Liam, please note that this is an old post an a new post is comming on this topic.

  • Tech Crates Sep 9,2012 at 11:13 am

    Was looking for exactly this issue you mentioned here in my blog. Finally used the wordpress typical add_filter / add_action logic. Thanks!

  • kristina Jul 17,2012 at 8:20 pm

    it would help greatly if you used to post your code. not only are the indents off, but it’s copying the smartquotes auto-generated by your blog resulting in tedious paste-in.

    thanks for the code, though!

  • san Dec 29,2011 at 8:01 pm

    Hi Banago,

    Thanks for your post.

    I want to limit contents through a page id. I mean I have a page where I want to print limited contents from different pages using page id in each separate section for each page.

  • Renee Nov 27,2011 at 11:17 pm

    Used Banago’s solution and it worked a treat for controlling how much text is displayed in some cluster style pages I have.

    Did encounter a problem though – each page of mine has an [intro] shortcode to style the first par. This function striped out any content that was in the shortcode.

  • vichet Jul 15,2011 at 2:37 am

    Thanks for sharing. I have made my own version. I would like to share it here. It can close html tags, show or hide images/shortcodes, re-enable auto video emed in wordpress, strip tags, read more links. Please visit it here:

  • mick warnes May 4,2011 at 2:32 pm

    Banago, I have spent a long while getting this to work properly in my site and have found your scripts to be the most useful. However, I lost access to my admin and on removal on our plugin, it started to work again, not sure why as it was working ok for a while.
    I then used the code above in my function file and this was doing it all apart from the ‘.’ were being stripped from the URL to the image, So I have removed this from your code
    $content = preg_replace(‘/[+]/’,”, $content);
    and it seems to work.
    I now feel that I have bodged it to work and wondered if it was a setting in WordPress that is the issue.

    • BANAGO May 4,2011 at 2:37 pm

      I’ve come up with a better solution that solves the problem wonderfully and is supper easy. Will write a new post for that. Stay tuned.

      • iTsMe Sep 3,2011 at 12:23 pm

        @ Banago i’ve followed this post for months for the better solution u say will provide, Please can kindly post it for us…Thanks

        • BANAGO Sep 11,2011 at 3:59 pm

          I’m sorry I have not been able too. Very busy here. I hope to do so in the very near future.

  • Brett Bumeter May 1,2011 at 10:44 pm

    I wasn’t happy with any of the above either. Went trolling for a solution that I could plugin to both StudioPress (client a) and c.bavota magazine premium (client b referred to me by client a).

    I still haven’t found the solution that could (plugin) to either theme, however, I did like the look of this plugin (that doesn’t work with either).

    Would like to find a way to make it work in either, or hard edit them to work with it (requires a change to Studiopress lib/formating.php file )

    still searching thats when I came across your article but didn’t see your own solution if it is published yet…

  • Martyr2 Jan 2,2011 at 10:24 pm

    Normally I don’t comment on code I find like this, but C.bavota’s “fix” is actually a bit clumbsy and full of problems.

    1) They tack on the ellipses but then strips it out using the preg_replace. It also strips out all periods in the content. [.+] matches one or more periods.

    2) Then what is with the line str_replace(‘]]>’, ‘]]>’, $content);? This is looking for ]]> and replacing it with ]]>? Useless line.

    Remove that last line and fix up the preg_replace and you will have a much more solid solution.

    • BANAGO Jan 11,2011 at 8:51 am

      If you want to hear my opinion, I don’t like any of those solutions, that’s why I have come up with my solution which I’m going to make public in a post soon.

  • relax tone Dec 29,2010 at 7:28 am

    Yes, this will allow you to do that. The code simply wraps your content in divs, so if you’re content looked like this:

  • Adi Sep 19,2010 at 8:47 pm

    Thanks for sharing I was searching for this.

    Now I can limit content in my blog.

    Thanks again

  • Evil Sep 18,2010 at 5:42 pm

    Ok that’s cool
    but how i can limit WordPress rss posts summary to 10 words only .

  • Majalah Sep 11,2010 at 10:49 pm

    Usually I use custome excerpt wp plugin to re-setting default wp excerpt. But, thanks for the tutorial.

  • Ali Sep 6,2010 at 2:17 pm

    Thank you very much for your kindness and help. I wish all the best for you.

  • BANAGO Sep 4,2010 at 12:00 pm

    Yes, please send me the the login data so that I can have a look what’s wrong.

  • Ali Sep 4,2010 at 8:47 am

    Dear Banago,
    I have asked my friend to give me a piece of his website to install wordpress and I did same on the real host. But it does not work! If you want I can give you the login address, admin and password to check the code. I really don’t know what is wrong. I have changed “the_content()” to “content(‘100’)” in loop.php.

  • Ali Sep 3,2010 at 2:29 pm

    Thanks for prompt answer. As a matter of fact, I am trying to test it on my PC (localhost, made by WampServer).
    I am gonna make a weblog for my school students. But I need to limit their posts up to 100 words only.

    And … I have left these optinons without any change:
    What do you want to appear after your excerpt?
    What do you want to appear after your content?
    What do you want your read more link to be?

  • ali Sep 3,2010 at 8:04 am

    Dear Banago, I have used “C.bavota’s Apprach”. I installed his latest plug-in. Then I went through loop.php and and changed “the_content()” to “content(‘100’)” which is my post limitation. But when I write a new post with 105 words it shows all!

    I even edited single.php with same changes. But there is no limitation on my posts.

    Can you help me please?

    • BANAGO Sep 3,2010 at 8:47 am

      Well, that’s very strange and I cannot guess an reason, unless I have a look at the website.

  • Cippo Aug 10,2010 at 6:16 pm

    I forgot to tell, the long code goes in functions.php and the $str and echo goes within your loop!


  • Cippo Aug 10,2010 at 6:15 pm

    Here’s another one!

    function word_limiter($text, $limit = 200) {
        // short enough?
        if( strlen($text) <= $limit) {
            return $text;
        // check if last word is full
        if( substr($text, $limit, 1) == ' ') {
            return trim( substr($text, 0, $limit));
        // hard cut
        $cut = substr($text, 0, $limit);
        // remove last word
        $words = explode(' ', $cut);
        return implode(' ', $words);

    and use it like this:

    $str = 'your string';
    echo word_limiter($str);

    Hope you find this useful!

    You can see it live on ( in latest comment <> 🙂

  • Sumon Jul 30,2010 at 8:22 pm

    Great Tricks @ Thanks for share this @!

  • graphicbeacon Feb 14,2010 at 9:45 pm

    It seems your comment form has cut off some of some of my code. Delete this if im wrong.

  • graphicbeacon Feb 14,2010 at 9:42 pm

    There is a bug with Studiopress’ approach. Either you or they did not escape some of the double quotes in the echo statements, resulting in some echo statement treated by php as a normal string(check the code). I made some corrections and displayed it as follows, adding the [title] and [rel] attributes so that it will help with validation:

    ', ']]>', $content);
    $content = strip_tags($content);
    if (strlen($_GET['p']) > 0) {
    echo "";
    echo $content;
    <a href="" rel="bookmark" title="Permanent Link to ">Read More →
    $max_char) && ($espacio = strpos($content, " ", $max_char ))) {
    $content = substr($content, 0, $espacio);
    $content = $content;
    echo "";
    echo $content;
    echo "...";
    <a href="" rel="bookmark" title="Permanent Link to ">
    else {
    echo "";
    echo $content;
    <a href="" rel="bookmark" title="Permanent Link to ">Read More →

Leave Your Comment

Your Comment*

Your Name*
Your Webpage