Excerpt or Content Word Limit in WordPress: Redux
by c.bavota | Posted in Tutorials | 91 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.
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/.



Hi,
Magazine Basic a very nice template, but I’d like to remove the excerpt/word limit funktion, but I’m not sure how to do this.
I’d like to be able to show the full post rather than the excerpt. Is this possible? And secondly – I’m not too familiar with coding, so would it be possible to get info on as to what I should change.
Thanks!
Hello Thassager,
In the newest version of the theme (2.3), just go to index.php and look for theme_excerpt() and getImage(). There should be three instances. Delete getImage() and replace theme_excerpt() with the_content() and that should do it.
Ah very good, was looking all over how to do this. Can a link to the content in question be included ?
Hi, is there anyway to increase the limit higher than 55?
I’m also curious if there is a way to increase the limit. Also, is it possible to include the entire post for the “Latest Story” and put a limit on the older posts?
First, thanks for a great theme – excellent job!
I, too, am interested in increasing the excerpt limit from 55 to something a bit higher. I have gone through the php code, and made several changes to no avail (now backed out) in an attempt to raise the value. I have also tried replacing themeexcerpt() with thecontent() in index.php as suggested above (to Thassager) just to remove the excerpts entirely (seems the underscore characters in this paragraph might be being filtered, FYI).
I’m just wanting the entire post to appear when a category is selected from the menu without having to click twice. I’m running 2.3.3 at present. Should I stay with this version or update to 2.4.9 and try to make the changes to that instead?
Any thoughts/help/guidance appreciated. Again, thanks.
Regards,
Jason
Fantastic work on this, however, I have not got it to work yet. Could I get a little more advice as to where this needs to go in the functions.php theme file?
Regards
Place it anywhere between
<?phpand?>.You’re a genius! Brilliant plugin.
Very cool plugin! Just as cool as Magazine Columns. Unfortunately they seem not to work together. Is there a way to merge both functions? I would really appreciate your advice because these plugins combined would save my current project… Help! Pleeease!!!!
How do i also exclude the < b > (bold) tag from posts?
that’s simple? thank you so much. Thought I need a plugin before. Wave the plugin now, it’s just need 17 lines
Nice! I tried the method on the WordPress site to shorten the excerpt and it didn’t work. I love how you can control each one individually this way too. Thanks!!
Argh, it was working until I updated to WP 2.8.2 . Anyone know of a way to work around that?
Hmm. I am using a version I have modified slightly on 2.8.x and it still works. Try this.
function theme_excerpt($num=55) { $link = get_permalink(); $limit = $num; $excerpt = explode(' ', strip_tags(get_the_excerpt()), $limit); if (count($excerpt)>=$limit) { array_pop($excerpt); $excerpt = implode(" ",$excerpt).'...'; } else { $excerpt = implode(" ",$excerpt); } $excerpt = apply_filters('the_content', $excerpt); $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt); echo '<p>'.$excerpt.'</p>'; }so anyway to keep formatting tags (ie
<p></p>) in the new output? thanks for the advice. i’m using your plugin on 2.8.How do i limit the words in my content in WP 2.8.2?
The plugin doesn’t seem to be working for me!
Sorry. Trying again. In WP 2.8.4, use
theme_excerpt and not just excerpt
in the code
<? php excerpt('25'); ?>It worked for me.
Remove the space between <? and php
I too would like to know how I can keep the paragraph formatting tags.
Just ad around the
just had the paragraph tag around the code
Just would like to say after trying a bunch of codes for the functions file and none of them working, yours worked the first time and I love it! Thanks!!
Hi there,
I love your code, but would like your help:
I wish to display only X number of words, but to keep ALL the formatting around them (e.g: the and so on)
Using
Wouldn’t do it.
Any other advice?
Thanks !
(p.s: consider adding the plugin “subscribe to comments”
Best,
Tal)
Hello, this function is terrific.
I am interested this additional question:
Is there a way to replace the [...] with a permalink to the post?
I figured out a work around to my question, simply by removing the … from the function, so line 15 looks like:
$content = implode(” “,$content).” “;
and using the permalink in the template instead. Thanks!
Also check out the new functions in WordPress 2.9 that allow you to modify the excerpt core function.
http://bavotasan.com/tutorials/quick-easy-excerpt-mods-coming-in-wordpress-2-9/
Thanks for the info…works perfectly for what I need!
Cheers
Stu
AWESOME! Thank you so much. The code works great in WP 2.9.1. It’s way better than the new function WP provides as it allows you to have different word counts on different pages.
Here’s a function based off of c.bavota’s to limit the number of characters as opposed to words. Example below is to return a shortened title. Should you want to shorten the excerpt or content by character count as opposed to word count just replace all the ‘title’ with ‘content’ or ‘excerpt’ respectively.
Also added in an if clause so it doesn’t add the … if its not longer than your specified character length.
Works in wordpress 2.9.1
Enjoy!
function short_title($num) { $limit = $num+1; $title = str_split(get_the_title()); $length = count($title); if ($length>=$num) { $title = array_slice( $title, 0, $num); $title = implode("",$title)."..."; echo $title; } else { the_title(); } }Very nice. Thanks for the code snippet.
thank you so much bro
i really was looking for something like this to short my title in my wordpress theme
thanks again
:D
you saved me
I know very little about PHP, and probably shouldn’t be messing with the code, but I really want to get this feature working on the site I’m building.
The thing I’m worried about is that the excerpt I’m trying to limit is not from the default text submission. I’m using the “Tubular” theme from StudioPress, and only the embedded video goes into the main “Edit Post” text box. The text goes into a custom text editor which appears below the main one when writing a new post. So my question is: what can I do to make this code work for this custom thingie. Here’s the snippet of code that…erm…echoes?…the content I’m trying to limit:
http://img39.imageshack.us/img39/201/codee.jpg
After I add the code to the functions document, do I just replace this code with something? If you go to my site, (don’t judge, it’s a work in progress), you can see how the “Newsies” review absolutely needs to be limited. Thanks for any help.
(FYI, the name of the custom text thingie is video description, which is why I assumed this was the part of the code I would have to replace.)
You can replace this line:
$excerpt = explode(' ', get_the_excerpt(), $limit);with this:
$excerpt = explode(' ', get_post_meta($post->ID, "_video_description", true), $limit);And that should work for you.
really need to get this working..
if a number of words in the content is less than the limit set.. it last word is being cut off.
For the instance, if my full content is “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc nunc lorem, interdum viverra pretium”
the result is “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc nunc lorem, interdum viverra…”
how would i solve this. anyone? really need a quick help..
btw… thanks for the function.. its awesome.. just have to fix this one issue
I just revamped it above, so check it out.