Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save celticwebdesign/b8848c8ec26d50dca7e05a8b2184faf3 to your computer and use it in GitHub Desktop.
Save celticwebdesign/b8848c8ec26d50dca7e05a8b2184faf3 to your computer and use it in GitHub Desktop.
WordPress permalink included.
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