This is an archived article. Please check my homepage for more up to date articles.
First, I am sure you can find a plug-in for Wordpress that performs this functionality as well, but I did not want to make this a “plug-in” per se, so I figured I would just give you a working snippet of code and allow you to take it from there. I needed to shorten the titles for a project I have been working on and I figured that you might find this useful as well. It is short, sweet and to the point so give it a try.
Next, I am assuming your Wordpress theme has a functions.php file within the theme directory, if not.. make one. It’s pretty simple, just make a new file within your wp-content/themes/
In that file, put the code:
function short_title($before = '', $after = '', $echo = true, $length = false) {
$title = get_the_title();
if ( $length && is_numeric($length) ) {
$title = substr( $title, 0, $length );
}
if ( strlen($title)> 0 ) {
$title = apply_filters('short_title', $before . $title . $after, $before, $after);
if ( $echo )
echo $title;
else
return $title;
}
}
?>
After you place this in your functions.php file, it makes the function “short_title();” available for use within your theme. Then, whenever you need the shorter version of your title, limited to a specific number of characters, you call
The variables are short_title(‘before’, ‘after’, true, ‘character limit’) the “true” is to echo it out, so you can leave it alone. If you wanted to have your titles have say… ** in front of them and ** after them, you could do short_title(‘**’,'**’,true, ‘41′); where 41 would be the number of characters before the **
Short, sweet, and to the point. Just a random something from my brain today.
Mar 31
This entry was posted on Tuesday, March 31st, 2009 at 12:44 pmand is filed under wordpress. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.The version of my website you are viewing is currently out of date. The content is still here as an archive and you are free to continue browsing it, however, I do recommend you check out my current homepage for more up to date information.
7 Comments Limit Characters on Wordpress Titles
Travis Quinnelly
March 31st, 2009 at 1:04 pm
1Nice job fella.
Steven
June 19th, 2009 at 9:34 am
2Hi, Nice script thanks! But I also want to do this with the content but it didn’t worked :( Can you help?
vendo
July 26th, 2009 at 5:58 am
3Greeting from Slovakia. Nice script. Working. Thanks
Nikolas
January 8th, 2010 at 6:25 am
4Hello. I am using Greek letters and I get a question mark in the end of the title. How can I avoid this ?
Thank you
tomek
January 17th, 2010 at 9:02 am
5Script is working fine, but some of local PL characters are scrambled, for example: ????ó – appear as question mark.
Khaled Hakim
February 23rd, 2010 at 4:35 am
6I just wanted to point out that I implemented your short titles code and I’m more than happy to say that it worked like a charm without any hassles. Concise and sharp way of presenting the information… keep it up… and keep these nice little posts coming :)
Phil
February 25th, 2010 at 6:01 pm
7Great code…
I added an additional feature to truncate partial words that may be cut off when you trim the title by modifying the first if statement.
if ( $length && is_numeric($length) && strlen($title) >= $length) {
$title = substr( $title, 0, $length ); // Trim title to 40 characters
$lastSpace = strrpos($title, ‘ ‘); // Locate the last space in the title
$title = substr($title, 0, $lastSpace); // Trim the portion of the last word off
}
RSS feed for comments on this post