Created
November 15, 2019 04:15
-
-
Save adamrosloniec/d606d68f652151242307005ae0cb44cf to your computer and use it in GitHub Desktop.
WordPress - The best way to get_the_excerpt with character limit
This file contains 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
// Source: https://stackoverflow.com/a/10173268 | |
function get_the_excerpt_with_characters_limit($text, $length) { | |
// Don't cut if too short | |
if (strlen($text) < $length + 10) { | |
return $text; | |
} | |
// Find next space after desired length | |
$break_pos = strpos($text, ' ', $length); | |
$visible = substr($text, 0, $break_pos); | |
return balanceTags($visible) . " …"; | |
} | |
// example usage: | |
echo get_the_excerpt_with_characters_limit(get_the_excerpt(get_the_id()), 160); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment