Limit the number of words in excerpt without plugins

Posted: August 28, 2013 at 4:54 pm

Excerpt by default display a numbers of characters so some times brake a word, No you can set limit the number of words displayed in the excerpt without any plugin.

Put the following code in your templates functions.php file.
[code]
function royal_string_limit_words($string, $word_limit)
{
$words = explode(‘ ‘, $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(‘ ‘, $words);
}
[/code]

Now put the following code in your template where you want to display the excerpt.
[code]
<?php
$excerpt = get_the_excerpt();
echo royal_string_limit_words($excerpt,55);
?>
[/code]
Where 55 is the number of words (not character) to display.