• 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 12 replies - 16 through 27 (of 27 total)
  • Thanks for that, Bechster – works a treat here 🙂

    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.

    You guys are grat, thank you very much

    thanks, I really appreciate it!!!..

    I love this forum!!

    [sig moderated]

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

    You guys are AWESOME!

    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.

    @bechster

    worked for me right away.. thanks

    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

    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!

    mujie

    (@mujie)

    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.

    Works great! Thanks beschster!

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