Shorten the text length in WordPress

function ShortenText($text,$chars_limit){
// Change to the number of characters you want to display
$chars_text = strlen($text);
$text = $text." ";
$text = substr($text,0,$chars_limit);
$text = substr($text,0,strrpos($text,' '));
// If the text has more characters that your limit,
//add ... so the user knows the text is actually longer
if ($chars_text > $chars_limit){
$text = $text."...";
}
return $text;
}
Advertisements