Created
October 26, 2013 00:44
-
-
Save FriendlyWP/7163956 to your computer and use it in GitHub Desktop.
Display content with a word limit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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