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.