apamphilon

Add … to a sting if over a certain character length (with out breaking words)

$the_title = get_the_title();

if ( mb_strlen( $the_title, 'utf8' ) > 50 ) {
   $last_space = strrpos( substr( $the_title, 0, 50 ), ' ' ); // find the last space within 50 characters
   $the_title = substr( $the_title, 0, $last_space ) . '...';
} else {
  $the_title = get_the_title();
}

credit


Comments: