Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save nextab/1840a0e84749e54697d9bcaff92a2f99 to your computer and use it in GitHub Desktop.

Select an option

Save nextab/1840a0e84749e54697d9bcaff92a2f99 to your computer and use it in GitHub Desktop.
Return no. of words for a given string (with option to strip tags or not)
// this function returns the string $text shortened to the number of words defined in the second attribute $length. It does not break words.
function nxt_truncate($text, $length = 30, $more = '...', $striptags = false) {
if ($striptags)
return force_balance_tags(html_entity_decode(wp_trim_words((wpautop($text)), $length, $more)));
else
return force_balance_tags(html_entity_decode(wp_trim_words(htmlentities(wpautop($text)), $length, $more)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment