Support » How-To and Troubleshooting » Limit the number of words in excerpt without plugins…

Limit the number of words in excerpt without plugins…

  • Hello everyone, as I wrote in the title I am trying to limit the number of words displayed in the excerpt without any plugin. Basicly, the problem is that I want to use the same excerpt in two different loops. The first one will display the whole excerpt, the second loop will only display a smaller part of it. Therefore I cannot limit the words number of ALL the excerpts, but I would need to do that locally. Ideally, if there is a solution to this I can use the same excerpts in many different places on the blog, using the same excerpt but in its longer/shorter version depending on the situation. Is that something impossible to do without having to use some weird plugins? Thanks everyone.

Viewing 15 replies - 1 through 15 (of 27 total)
  • whooami



    <?php $little_excerpt = substr(the_excerpt(),0,XY); ?>

    where XY is the limit, you ought to be able to use that in any template file just fine. If its not quite right, look at the php man page for substr

    that’s an excellent way to get a character limit on your excerpt.

    a fairly easy way to get a word limit, though, is to use the_content_rss instead of the_excerpt. That function apparently allows you to specify a word limit.





    oh yeah oops, words not chars ๐Ÿ˜› (my bad)

    I’d rather try and keep using the excerpt for layout purposes, I’ll try to look around for a way to use substr to limit words instead of chars… in the mean time if anyone else got good idea… I am here!
    Thanks guys ๐Ÿ™‚

    then use the_excerpt_rss, if you must…

    I don’t understand why the_content_rss doesn’t do it for you, unless maybe you’re writing your own excerpts.

    <?php $little_excerpt = substr(the_excerpt(),0,XY); ?> doesn’t work because the_excerpt() is not at string. Use get_the_excerpt() instead, that will do the trick. However, this is still limiting characters, not words.

    I just found a solution to limiting the number of words in the excerpt without plugins. Put the following piece of code in your templates functions.php file:

    function string_limit_words($string, $word_limit)
      $words = explode(' ', $string, ($word_limit + 1));
      if(count($words) > $word_limit)
      return implode(' ', $words);

    Next, put the following piece of code in your template where you want to display the excerpt:

      $excerpt = get_the_excerpt();
      echo string_limit_words($excerpt,25);

    Where 25 is the number of words to display.

    Happy coding!



    hi bechster,

    i tried to copy&paste your code โ€“ย but โ€“ย didnt work. i am not a hardcore coder. is there a special spot where i should paste it? at the beginning of the function/index page or at the end, for e.g.?

    thanks for your help ๐Ÿ™‚



    Hey guys, it worked for me like a charm. I just created a functions.php file I didn’t have, and put there the first part of bechster code, then, in my index.php, where I need to show tiny excerpts (and instead of a classical <?php the_excerpt(); ?>), I pasted the second part of the code.

    I’m using the last WP version.


    Thanks! I confirm that your code works perfectly. You guys are amazing! Thanks for starting this thread, I just googled what I need and voila :).




    I confirm that it works. Simple snippet, and very useful to strip words… Thanks!

    Hi bechster,

    Thanks for your code! It works quite fine.

    Greetings from Belgium.



    Good, bechster,
    I used the_content_rss and substr(get_the_excerpt() with results, but always words got cut.

    Your function string_limit_words is great, easy to use and nice results.


    Doesn’t work for me…
    I put the bechster code into the first line of my functions.php.
    No error until now, but:

    But when I put the little piece of code in order to display the excerpt (into sidebar.php), I receive an error:
    Warning: Cannot modify header information – headers already sent by (output started at /home/met830/public_html/wp-content/themes/x-theme/functions.php:11) in /home/met830/public_html/wp-includes/pluggable.php on line 694

    What happened?

    Hi MarcOliver and karakosta

    I’m not quite sure why you are getting errors. However I suspect you may have put the excerpt in the wrong place.

    Please make sure you put the excerpt within the loop.

    Look for the first and last line of the code below in your template and place the excerpt somewhere in between. Like this:

    <?php while (have_posts()) : the_post(); ?>
      <!-- Put the excerpt here -->
        $excerpt = get_the_excerpt();
        echo string_limit_words($excerpt,25);
      <!-- End the excerpt -->
    <?php endwhile; ?>

Viewing 15 replies - 1 through 15 (of 27 total)
  • The topic ‘Limit the number of words in excerpt without plugins…’ is closed to new replies.