WordPress Business Solutions

HIRE A WORDPRESS TECHNICIAN & WEBSITE DESIGNER

Contact Us
Taumata-city

When a post title is simply too long…

Lately I had to find a solution where a client had an habit of writing endless titles to it’s blog posts. While my job is not to judge client’s habits, the website’s homepage was looking pretty awful with these long titles.

So the solution I had to come up with needed to automatically truncate each post titles on the homepage and archives pages, but leave the full title on single post pages. Now I’ll show you how I did it with the help of a PHP code snippet.

a) I simply added the following PHP code to the theme’s functions.php file. It’s easy, so just try it.


function custom_trim_my_title( $title ) {
if ( strlen( $title ) >= 72 && ! is_singular() ) {
$title = substr( $title, 0, 72 ) . '...';
return $title;
}
return $title;
}
add_filter( 'the_title', 'custom_trim_my_title' );


b) Now you can customize the title’s length by changing the 72 variable to any number of letters you wish to display.

Once the PHP code is included in your functions.php file, each truncated titles will now be followed with “…” at the end.

If you are not comfortable in adding this PHP code by yourself, simply ask a WordPress Specialist that will do it for you.



Share this page:
Facebooktwittergoogle_plusredditlinkedinFacebooktwittergoogle_plusredditlinkedin