Feb
16
2009

Excerpt or Content Word Limit in WordPress: Redux

by   |  Posted in Tutorials  |  85 comments

This is just a revamp of a function I wrote a while back to add the ability to limit the number of words displayed when you call the excerpt or content. As it stands, the max number of words for the excerpt is 55. There is a new function with WP 2.9 to increase this number. Check out Quick & Easy Excerpt Mods Coming in WordPress 2.9 for more on that subject.

Add the following code to your functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $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) {
    array_pop($content);
    $content = implode(" ",$content).'...';
  } else {
    $content = implode(" ",$content);
  }	
  $content = preg_replace('/\[.+\]/','', $content);
  $content = apply_filters('the_content', $content); 
  $content = str_replace(']]>', ']]>', $content);
  return $content;
}

Now, instead of using the_content() or the_excerpt in your loop, use excerpt($limit) or content($limit).

If you want to limit your excerpt to 25 words the code would look like this:

<?php echo excerpt(25); ?>

I also created a plugin for this that you can download at http://wordpress.org/extend/plugins/content-and-excerpt-word-limit/.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

Tags: , , , , , , , , , , , , , , , , , , , , , ,

Short URL: http://bit.ly/90JpsA

Discussion 85 Comments

  1. Dyce on February 12, 2010 at 8:32 pm

    So I guess I’m the only one getting an “unexpected T_FUNCTION” error…

    I copied the code straight from here. Any ideas on why it’s not working?

  2. Dyce on February 12, 2010 at 8:59 pm

    i give up.

    • c.bavota on February 13, 2010 at 9:53 am

      What line gives you the error?

  3. gedelis on February 16, 2010 at 12:48 pm

    Thank you very much for the code!!! I was looking for a similar code over the internet for a few hours and then BAM your website! I’m glad to see that this is very up-to-date code(WP 2.9). :) Works fine on WP 2.9.2 .

    Keep creating good snippets.

  4. Louis on February 17, 2010 at 8:57 am

    Hi I dont want the … to show at all neither any thing ells just the read more what must I do.
    Thanx
    Louis

    • Louis on February 17, 2010 at 9:06 am

      never mind I got it to work my way thanx for the great post

  5. DirkJohanson on February 20, 2010 at 3:38 pm

    When I get this installed, this will be a fantastic addition to my site, and I thank you for coming up with it.

    One question. The installation instructions state to “Upload the excerpt-content-word-limit folder to the /wp-content/plugins/ directory.” I can’t find any such directory on my computer, including after searching for it in my system as a possible “file or folder,” so I’ve been unable to upload.

    Please let me know if there any place this directory would typically be, or if and how I could create one.

    • c.bavota on February 22, 2010 at 1:57 pm

      The code above needs to be added to your function.php file which is in your theme’s directory. If you don’t have one create one. If you are installing the plugin and you don’t have a wp-content/plugins folder then you probably don’t have WordPress installed correctly.

  6. dave on March 2, 2010 at 4:41 pm

    Cheers! Very easy to implement and a huge help for me. The details are what make WordPress such a pleasure to use.

  7. Ozai on March 15, 2010 at 1:11 am

    Bavota,
    thanks for the plugin.. however most of the comments here make me feel more frustrated if not DUMB!
    has EVERYBODY on Earth become a GEEK??? or is it just the forum?
    People here are discussing PHP like the weather! even those who have photos of Old women??? what the hell??

    anyway.. I did install the plugin, and I can’t seem to go anywhere past that stage:
    When you mention in your plugin that:

    ———-
    Instead of the_excerpt() or the_content(), use or within your loop to limit the words for each.

    Example: .

    ———-

    what do you exactly mean?
    where should I place those lines of code? and what’s the definition of a LOOP? and how do I get to it?
    Thanks for your patience..
    Peace….

    • c.bavota on March 15, 2010 at 3:33 pm

      The loop is a WordPress function that “loops” through your posts and gathers all the information that you want to display. It is the if (have_posts()) : while (have_posts()) : the_post(); piece of code you usually see in your theme files. To place something within the loop means to put the code within the segment before the endif; statement.

      For this plugin, you need to replace the default excerpt and content functions with the functions indicated above or it will not work properly.

  8. Jenn on March 15, 2010 at 7:33 pm

    Thank you SO much for this function! I’m curious, though, with the content limiter, is it possible to have it remove images from the post’s content?

  9. Kathy on May 4, 2010 at 8:47 pm

    Cool little snippet there. Do you know if anyone has a decent snippet for making an excerpt sentence-aware?

  10. gino on May 10, 2010 at 10:15 am

    Hi there

    I have been trying your plugin and it’s working but it’s overriding the display only X posts and showing all the available posts.

    Do you know how to stop this?

    thanks

  11. gino on May 11, 2010 at 4:52 am

    basically I only want the feed to show part of one post in an HTML site

    your code is great but it ends showing all the posts overriding WP settings to that effect and my ruining my page layout as a result!

    hope this makes sense

    thanks again

    cheers

    g

  12. Zoran on May 25, 2010 at 4:06 pm

    Thanks for the code… All the best from Serbia…

  13. Mustamar Natsir on June 2, 2010 at 2:56 pm

    Great! Thanks for the code. This is awesome and really helpful.

  14. Oleh on June 10, 2010 at 7:57 pm

    Hi, I’ve tried excerpt(); content(); and the_content_limit(); instead of regular the_content(); but I need something different. It’s so simple, I can’t believe everyone else would not want this. So I want some $limit of characters/words(whatsoever) including image(s). Many blog posts over globe have image at the beginning.
    The content(); does show images, but it also grabs HTML, and in some posts some opening tags being pooled to listing page without closing, and so page get broken. So I need to pool only $limit ‘ed text AND images. Ideas?

  15. jack on June 24, 2010 at 2:45 am

    great plugin, tried many similar ones, but this is the only one that has done it correctly.

  16. kevin on June 24, 2010 at 4:53 am

    Very cool plugin!.. Can this work for newest version of wordpress (3.0)

    Thanks.

  17. paul on July 14, 2010 at 8:24 pm

    thanks for the great tip here, but I still can’t get the read more link to work. i tried using yur modified code from the forum but when I add that code, the website does not load. just get a blank page…. what is the full code to use to add the read more link?

  18. Yabbox on July 15, 2010 at 12:21 am

    Hi,

    This excerpt limit length is confusing me. I have WP3 and everywhere I see the limit length is 55, but it is definitely being limited to 20 words. Is this my theme (arras) or a change in WP3?

    Also, I would like to add a limit length to a get_the_title on a caption, is there a code to set the length of the call in certain cases, or is that getting too tricky?

    Thanks,
    Andy

  19. Yabbox on July 15, 2010 at 12:32 am

    Hi,

    Sorry, ignore my first post.

    I would like to add a limit length to a get_the_title on a caption, is there a code to set the length of the call in certain cases, or is that getting too tricky?

    Thanks,
    Andy

  20. M.W.M on July 31, 2010 at 11:49 pm

    Thanks for your cool tips!

    I use the function content($limit) and made changes in preg_replace to be like this:

    $content = preg_replace('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i','', $content);

    That code will remove first image in the post, with tag:

    $content = preg_replace('/<a.+href=[\'"]([^\'"]+)[\'"].*><img.+src=[\'"]([^\'"]+)[\'"].*><[\/]a>/i','', $content);

    But, it still can’t remove an image which have anchor tag, like:

    <a><img></img></a>

    What should I put in the code so the both image with or without anchor tag will be removed?

    *sorry for my english*

  21. girls on August 14, 2010 at 9:41 am

    Thank you but how do I get the more link to appear when using this <?php echo excerpt(25)"

    Thanks.

  22. James on September 7, 2010 at 5:25 pm

    Thanks for the php code. I just looking for plugin to do it, now it make it simple without the plugin. But I wonder, can this code be modified so it will cut some word at the beginning of excerpt too?

  23. Chris on September 16, 2010 at 6:45 pm

    I think this code really does wonders, however, is it possible to size the images that come in the content?

    that would be a breakthrough.

    Thanks

  24. lwinmemehtun on October 13, 2010 at 3:31 am

    Thank

  25. Scott Pelland on November 21, 2010 at 4:17 pm

    Thanks for the super excerpt length code. I made a small modification to automatically insert a link to the post with a “…read more” link following the excerpt that I thought I would share. You can see the code below. I also tested this in WP 3.01 and extended the excerpt length to 100 words using the new_excerpt_length function.

     
            $excerpt = implode(" ",$excerpt).'<a href="' . $link . '">...read more</a>';