We might have a situation, when we need to display the excerpt with the fixed number of world. To achieve that, create the next function in functions.php of the current theme:
function do_excerpt($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if (count($words) > $word_limit)
array_pop($words);
echo implode(' ', $words).' ...';
}
After, to output the the post excerpts in the templates file create next code:
<?php do_excerpt(get_the_excerpt(), 50); ?>
50 is number of words to display.
If the wordpress version 2.9 or higher is used, we can just use next code snippet.
function new_excerpt_length($length) {
return 50;
}
add_filter('excerpt_length', 'new_excerpt_length');
The code above must be in the function.php file.
Leave a trackback from your own site.
Leave a Reply
You must be logged in to post a comment.
3 Responses to “Display Excerpt With Fixed Number of Words”
Hi
I’ve ended up here because I’ve downloaded the AdSimple free template and the author’s Russian so I can’t contact him, so was trying to figure out how to make my excerpts longer on the homepage…. he’s kind of used this method, but somewhere something’s not quite right as changing the excerpt number from his default 55 to anything else makes no difference.
Which is annoying.
I just wish things would work :)
Good blog you have!
nice
True words! Also test ; might be useful when it comes to directory submission