Skip to content

Instantly share code, notes, and snippets.

@jfeliweb
Created October 16, 2014 17:24
Show Gist options
  • Save jfeliweb/556e77dd50070ddd6855 to your computer and use it in GitHub Desktop.
Save jfeliweb/556e77dd50070ddd6855 to your computer and use it in GitHub Desktop.
Controlling Content Length Using A Custom Function
/*************************************************************************
The following function takes two parameters:
$string – The string that needs be limited for example get_the_excerpt().
$max_words – The maximum amount of words you would like to show.
***************************************************************************/
/* Limit Your Website Excerpt */
function word_limit($string, $max_words){
$post_words = explode(' ', $string);
$count = count($post_words);
$post_words = implode(' ', array_slice($post_words, 0, $max_words));
if($count > $max_words):
return $post_words . '...';
else:
return $post_words;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment