Feb
16
2009
Excerpt or Content Word Limit in WordPress: Redux
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/.




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?
i give up.
What line gives you the error?
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.
Hi I dont want the … to show at all neither any thing ells just the read more what must I do.
Thanx
Louis
never mind I got it to work my way thanx for the great post
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.
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.
Cheers! Very easy to implement and a huge help for me. The details are what make WordPress such a pleasure to use.
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….
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 theendif;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.
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?
Nevermind! Just found it in another of your tutorial postings: http://bavotasan.com/tutorials/removing-images-from-a-wordpress-post/
Thanks a million!
Cool little snippet there. Do you know if anyone has a decent snippet for making an excerpt sentence-aware?
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
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
Thanks for the code… All the best from Serbia…
Great! Thanks for the code. This is awesome and really helpful.
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?
great plugin, tried many similar ones, but this is the only one that has done it correctly.
Very cool plugin!.. Can this work for newest version of wordpress (3.0)
Thanks.
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?
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
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
Thanks for your cool tips!
I use the function content($limit) and made changes in preg_replace to be like this:
That code will remove first image in the post, with tag:
But, it still can’t remove an image which have anchor tag, like:
What should I put in the code so the both image with or without anchor tag will be removed?
*sorry for my english*
Thank you but how do I get the more link to appear when using this <?php echo excerpt(25)"
Thanks.
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?
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
Thank
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>';