Created
June 15, 2017 11:07
-
-
Save celticwebdesign/b8848c8ec26d50dca7e05a8b2184faf3 to your computer and use it in GitHub Desktop.
WordPress permalink included.
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
echo limit_text( get_field('village_finds_report'), 40, get_permalink( get_the_id() ) ); | |
// originally taken from | |
// https://stackoverflow.com/questions/965235/how-can-i-truncate-a-string-to-the-first-20-words-in-php | |
// slightly adjusted to include a url. | |
function limit_text($text, $limit=40, $url='') { | |
if (str_word_count($text, 0) > $limit) { | |
$words = str_word_count($text, 2); | |
$pos = array_keys($words); | |
$text = substr($text, 0, $pos[$limit]) . '...'; | |
if( $url ){ $text.= ' <a href="'.$url.'" class="view-article">View Article</a>'; } | |
} | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment