Wordpress Technology

Wordpress Tips, Tutorials, Hacks

Display Excerpt With Fixed Number of Words

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.


Both comments and pings are currently closed.

© 2009 | Made by Vancouver Web Design | Internet Marketing