Skip to content

Instantly share code, notes, and snippets.

@FriendlyWP
Created November 18, 2013 20:33
Show Gist options
  • Save FriendlyWP/7534825 to your computer and use it in GitHub Desktop.
Save FriendlyWP/7534825 to your computer and use it in GitHub Desktop.
Limit words in excerpts
/***
**** LIMIT WORDS IN EXCERPTS (or any supplied content)
Usage:
$text = get_the_excerpt();
echo string_limit_words($text, 40);
*/
function string_limit_words($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment