WordPress.org

Ready to get started?Download WordPress

Forums

Limit the number of words in excerpt without plugins... (28 posts)

  1. jockerworks
    Member
    Posted 5 years ago #

    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.

  2. whooami
    Member
    Posted 5 years ago #

    <?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

  3. Ivovic
    Member
    Posted 5 years ago #

    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.

    http://codex.wordpress.org/Template_Tags/the_content_rss

  4. whooami
    Member
    Posted 5 years ago #

    oh yeah oops, words not chars :P (my bad)

  5. jockerworks
    Member
    Posted 5 years ago #

    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 :)

  6. Ivovic
    Member
    Posted 5 years ago #

    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.

  7. bechster
    Member
    Posted 5 years ago #

    <?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.

  8. bechster
    Member
    Posted 5 years ago #

    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:

    <?php
    function string_limit_words($string, $word_limit)
    {
      $words = explode(' ', $string, ($word_limit + 1));
      if(count($words) > $word_limit)
      array_pop($words);
      return implode(' ', $words);
    }
    ?>

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

    <?php
      $excerpt = get_the_excerpt();
      echo string_limit_words($excerpt,25);
    ?>

    Where 25 is the number of words to display.

    Happy coding!

  9. MarcOliver
    Member
    Posted 5 years ago #

    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 :-)

  10. Dunkkan
    Member
    Posted 5 years ago #

    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.

  11. abelq
    Member
    Posted 5 years ago #

    Bechster,

    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 :).

  12. cutomthemedesign
    Member
    Posted 5 years ago #

    Bechster,

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

  13. jk_
    Member
    Posted 5 years ago #

    Hi bechster,

    Thanks for your code! It works quite fine.

    Greetings from Belgium.

  14. longjorn
    Member
    Posted 5 years ago #

    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.

    Thanks!

  15. karakosta
    Member
    Posted 5 years ago #

    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?

  16. bechster
    Member
    Posted 5 years ago #

    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 -->
      <?php
        $excerpt = get_the_excerpt();
        echo string_limit_words($excerpt,25);
      ?>
      <!-- End the excerpt -->
    <?php endwhile; ?>
  17. blogmum
    Member
    Posted 5 years ago #

    Thanks for that, Bechster - works a treat here :-)

  18. LazyAndroid
    Member
    Posted 5 years ago #

    The first answer from whooami actually helped me, because that was just what I was searching, to limit the characters not words. Except it did not work. After looking at bechster code and investigating a bit I came up with this:

    <?php echo substr(get_the_excerpt(),0,XY); ?>

    All that instead of <?php the_excerpt() ?> and for XY type the number of chars desired. Hope it's helpfull to some other PHP noob like me.

    Of course my code looks a bit different:

    <?php
    	$content = get_the_content('',FALSE,'');
    	$content = apply_filters('the_content', $content);
    	$content = str_replace(']]>', ']]>', $content);
    	echo substr($content,0,262);
    ?>&hellip;

    At the end of the last paragraph a none-linking … is inserted with the code &hellip;. I have no idea why this works. Logic tells me it should appear outside the <p> tag. But it's inside and it works (and validates), so it's fine with me. I still would like to hear a logic answer if someone can offer one.

  19. futureman
    Member
    Posted 5 years ago #

    You guys are grat, thank you very much

  20. boosey12
    Member
    Posted 5 years ago #

    thanks, I really appreciate it!!!..

    I love this forum!!

    [sig moderated]

  21. marketingppt
    Member
    Posted 5 years ago #

    Wow I needed to do be able to do this with my excerpts. This is a great resource

  22. chiahau
    Member
    Posted 5 years ago #

    You guys are AWESOME!

  23. thetsp
    Member
    Posted 5 years ago #

    Thanks for great works.
    For beginners like me, be careful for the file name.
    It's not function.php, it's functions.php.(Don't forget s)
    If its wrong, it doesn't function properly.

  24. serialboxhpc
    Member
    Posted 5 years ago #

    @bechster

    worked for me right away.. thanks

  25. jami1955
    Member
    Posted 5 years ago #

    Fabulous, perfect, great code. One question:
    I want to post excerpts of my Recent posts, as in the last three posts. In this code

    <?php
      $excerpt = get_the_excerpt();
      echo string_limit_words($excerpt,25);
    ?>

    is there something I could put in that string which would specify LIST the excerpts from the last three posts with titles either as links, or with the Read more link at then end?
    thanks!
    JSC

  26. canchuckwood
    Member
    Posted 5 years ago #

    Hi guys

    Great piece of code - thanks! I'm wondering if anyone can help me adapt it for something. I want to list a limited excerpt which changes size...

    Eg. in my main excerpt I have

    <div class="post-meta">Dogs Bollix | 18/10/2008</div>
    Mostly upbeat songs, their enthusiasm is reflected in a tight and energetic show, and in the crowd at the Dogs Bollix all eyes were riveted to the stage.

    Which could change to

    <div class="post-meta">San Francisco Bathhouse | 16/10/2098</div>
    Mercury Crowe are hot hot hot. Did I say hot? They are young, talented, their live show is on fire, and they are up-and-coming in the best sense of the phrase.

    (Dogs Bollix and San Francisco Bathhouse are music venues in NZ and I am building a music info website).

    I want to display only the venue and date information on a different part of the site. So is there any way I can adapt Bechsters code to display only the div rather than limit the number of words? I'm a novice at php so any help is appreciated!

    Thanks!

  27. mujie
    Member
    Posted 4 years ago #

    I was add a little tricky for bechster code :

    <?php
    function string_limit_words($string, $word_limit)
    {
      $words = explode(' ', $string, ($word_limit + 1));
      if(count($words) > $word_limit) {
      array_pop($words);
      //add a ... at last article when more than limit word count
      echo implode(' ', $words)."..."; } else {
      //otherwise
      echo implode(' ', $words); }
    }
    ?>

    So, when your word count more than a limit, you get a ... at last article.

    Hope it's usefull.

  28. johnhurr
    Member
    Posted 4 years ago #

    Works great! Thanks beschster!

Topic Closed

This topic has been closed to new replies.

About this Topic