Created
October 21, 2021 12:53
-
-
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 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
| // 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