At the moment i have a function and a filter which shortens titles if they are longer than 25characters and appends the triple dot '...'
However, it appends the dots to all the titles. How can i get the following code to run only if the title is longer than 25 characters?
function japanworm_shorten_title( $title ) {
$newTitle = substr( $title, 0, 25 ); // Only take the first 25 characters
return $newTitle . " …"; // Append the elipsis to the text (...)
}
add_filter( 'the_title', 'japanworm_shorten_title', 10, 1 );
You can use PHP's strlen
: http://php.net/manual/en/function.strlen.php
Your code would go like:
function japanworm_shorten_title( $title ) {
if (strlen($title) > 25){
$title = substr( $title, 0, 25 ).'…';
}
return $title;
}
add_filter( 'the_title', 'japanworm_shorten_title', 10, 1 );