Skip to content

Instantly share code, notes, and snippets.

@FriendlyWP
Created October 26, 2013 00:44
Show Gist options
  • Save FriendlyWP/7163956 to your computer and use it in GitHub Desktop.
Save FriendlyWP/7163956 to your computer and use it in GitHub Desktop.
Display content with a word limit
/**
* Display specified content with a word limit
* Can be used for excerpts, full content, etc.
* In your theme file, use thus to display 40 words of the excerpt:
* $excerpt = get_the_excerpt();
* echo string_limit_words( $excerpt, 40 );
*
* @param string $string A variable containing the content you wish to display.
* @param num $word_limit The number of words to display.
* @return string Content limited to the number of words specified.
*/
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