Do you ever got an issue to fit your blog post titles in limited width space. Or if every your client asks you to display only first 100 characters of the blog posts title. If yes then you are at right place, as in this post I am sharing a best trick to truncate post title in wordpress blog, without using any plugin.
To implement truncated post title you simply have to locate the file containing post title. This all depends on the WordPress theme you are using on your site, but most provably the title code will located in index.php of your current activated theme folder.
Implementing Truncated Post Title
1. Simply open index.php from your current activated theme
2. Search for anchor tag with post title. Code might look something like this –
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>. Or you can simply search for <a href="<?php the_title(); ?>
3. Replace complete anchor tag with the below code
<a href="<?php the_permalink() ?>"> <?php $thetitle = $post->post_title; $getTitlelength = strlen($thetitle); $titlelength = 100; echo substr($thetitle, 0, $titlelength); if ($getTitlelength > $titlelength) echo "..."; ?> </a>
Just don’t forget to adjust the character limit in the above code as per your requirement, as in above code I have assigned “100” as a character limit. So the above code will going to display first 100 characters of you blog post title following with “…”
You can also directly place this code in any of the theme file where you want to display truncated post title.