Limit number of words in WordPress excerpt without plugins


To limit number of words in the excerpt without plugins. Put the following piece of code in your templates functions.php file:

<?php
function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}
?>

Next, put the following piece of code in your template where you want to display the excerpt:

<?php
  $excerpt = get_the_excerpt();
  echo string_limit_words($excerpt,25);
?>

Where 25 is the number of words to display.

To limit number of characters in the excerpt without plugins. Put the following piece of code in your templates file:

<?php echo substr(get_the_excerpt(),0,XY); ?>

Where XY represents the limit upto which you need to display the text.

And You are all done!

Happy coding!