8
Jan
Quick Tip #8 - Limit the length of ‘the_excerpt’ in WordPress
Posted by Nouveller in Quick Tips
Posted by Nouveller in Quick Tips
Right here’s the scenario. On one of your clients post page’s you use the_excerpt function to display an excerpt for each post. You’ve got room for a decent size excerpt and explain to them they can put about a paragraph into each excerpt, 100 words or so.
They then ask for a mini news feed that they want to appear on the homepage. You go ahead and add it and all of a sudden you’ve got paragraphs of text cluttering up the home page. You can either now go and tell them they can only use 10 words in each excerpt (which will look ridiculous on the posts page) or explain to them how to create a custom field which is an excerpt of the excerpt… or…
Rather than confuse them we’re just going to create a really simple way to limit the amount of words when displaying the_excerpt, we’re not limiting the word count from the posts edit screen remember. Just the small excerpt that we want to appear in mini news feed on the home page, only about 10 words.
Let’s start by writing our function that will shorten the_excerpt. Open up functions.php from your theme folder and add this function in. The explanation is commented for you.
function limit_words($string, $word_limit) { // creates an array of words from $string (this will be our excerpt) // explode divides the excerpt up by using a space character $words = explode(' ', $string); // this next bit chops the $words array and sticks it back together // starting at the first word '0' and ending at the $word_limit // the $word_limit which is passed in the function will be the number // of words we want to use // implode glues the chopped up array back together using a space character return implode(' ', array_slice($words, 0, $word_limit)); }
I’m doing this in the mini news feed loop. So find:
<?php the_excerpt(); ?>
And replace it with this:
<?php echo limit_words(get_the_excerpt(), '10'); ?>
First we echo our function (as it returns a value), we then pass the string through that we want to chop. You have to use get_the_excerpt as this returns the excerpt rather than echo’ing it like the_excerpt does. Finally we pass the number of words we want, in this case 10.
Voila! Easy right? Have any questions please leave them in the comments.
My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.
This entry was posted on Friday, January 8th, 2010 at 1:25 pm and is filed under Quick Tips & tagged with . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Spyro wrote on January 24, 2010
Hi , unfortunately I can’t do it
… I post the function in the functions.php and then I replace the with the one you say above and i get fatal error. what do i do wrong? check this out - http://thenoughtiespage.info/tag/madonna/
Nouveller wrote on January 24, 2010
Are you running PHP 4 or above?
Ronaldo Bitencourt wrote on January 29, 2010
helpful!
Tkss.
Jay Tillery wrote on February 14, 2010
Isn’t it easier to just use the_content_rss(’more…’, ”, ”, 10); to limit the number of words on the homepage? That way you don’t get that [...] and you can place whatever you want for the more text.
Nouveller wrote on February 16, 2010
It would be easier yes but It’s more about learning how the script works and how you could then apply it else where.
Jim wrote on March 25, 2010
the_content_rss is depreciated, which means it is not a long term solution
Ttravis Stowe wrote on March 26, 2010
if you want to have the [...] after the words. All you have to do copy this.
[...]
All you have to do is place the [...] after the echo function. It will show up right after the limited word count you choose. I replaced the ‘10′ word count with ‘150′ to give more wording to pull.
Enjoy!
Ttravis Stowe wrote on March 26, 2010
oops.
the code despaired. : )
echo limit_words(get_the_excerpt(), ‘150′); ?> [...]
Nouveller wrote on March 27, 2010
Thanks for the tip!
Thiramiro wrote on May 7, 2010
Works perfectly.
Thanks!!!
age wrote on May 26, 2010
thx for the tip,
but.. how if some post doesn’t have any excrept?
i can not simply wrote:
the word is still cut into default WP 55 chars (with the [...]
?
age wrote on May 26, 2010
update ->the code is:
if (the_excrept!==”"){
echo limit_words(get_the_excerpt(), ‘25′);
}else{
echo limit_words(the_content, ‘25′);
}
Ttravis Stowe wrote on May 26, 2010
nice, I didn’t think about that. thanks for the share.. : )
Gojeg wrote on May 27, 2010
Nice trick! I will try it..
Kawauso wrote on August 2, 2010
http://codex.wordpress.org/Function_Reference/the_excerpt#Control_Excerpt_Length_using_Filters
No need to rewrite the wheel.
Also see:
http://codex.wordpress.org/Function_Reference/the_excerpt#Remove_.5B….5D_string_using_Filters
Nouveller wrote on August 3, 2010
No there isn’t, but you can apply the technique in other applications - not just WordPress. It’s good to know how things work.
Nouveller wrote on August 3, 2010
Plus I think what you posted will globally change the_excerpt. In this case I needed to modify one particular excerpt without changing the other.
wp-popular.com » Blog Archive » Quick Tip #8 – Limit the length of the_excerpt’ in WordPress | Nouveller wrote on August 14, 2010
[...] more: Quick Tip #8 – Limit the length of the_excerpt’ in WordPress | Nouveller Tags: excerpt, functions, php, result, [...]
Mike Freeman wrote on August 17, 2010
Awesome, thanks! What about changing it so it shows characters, not words?
Nouveller wrote on August 17, 2010
Thanks for the comment Mike, you should check out the substr PHP function, that should do away with any un-wanted characters.
sagive wrote on August 23, 2010
thanks man.. exactly what i searched for. maybe you could include in this post a link to add “read more” link in the end