Apr
06
2010
Quick Function to Shorten the Post Title in WordPress
For some reason, a lot of people seem to be using extremely long post titles. I am a firm believer in making a title as precise as possible to make it easier on people who are trying to actually find your article. Sure, I guess you can throw in as many keywords as you want in hopes that you might get more from Google, but I doubt that will really do anything for your site’s SEO.
In case you are one of those people who feel the need for extremely long post titles and you want to display them in places where they might not fit, here is a quick piece of code that you can add to your functions.php file.
function short_title($after = '', $length) { $mytitle = explode(' ', get_the_title(), $length); if (count($mytitle)>=$length) { array_pop($mytitle); $mytitle = implode(" ",$mytitle). $after; } else { $mytitle = implode(" ",$mytitle); } return $mytitle; } |
You can call the function like so:
<?php // short_title($after, $length) echo short_title('...', 10); ?> |
The above code will truncate your title at 10 words and add a trailing ellipsis.



This is a common problem as sometimes our titles do become very long and don’t fit nicely. Great piece of code which we will try out. Thank you!
I too like to use short titles..
Hey thanks for this piece of code! I tend to go on a bit, particularly in my titles, so this piece of code is just what I need! I’m glad someone took the initiative!