How to Truncate WordPress Post Titles with PHP

Posted on August 16th, 2010 by in Themes | 17 Comments  
How to Truncate WordPress Post Titles with PHP

While working on our client’s site, the design’s width would not allow us to keep the long titles to be displayed on the homepage. After searching the web for options, we came across several plugins that would truncate WordPress titles, but they would do it sitewide whereas we only wanted it on the homepage. In this article, we will share with you how you can truncate WordPress Post Titles with PHP.

First open your index.php or the file location where you want to truncate the title. Then paste the following code to replace your the_title tag.

<a href="<?php the_permalink() ?>">
<?php
$thetitle = $post->post_title; /* or you can use get_the_title() */
$getlength = strlen($thetitle);
$thelength = 25;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo "...";
?>
</a>

Make sure you edit the $thelength variable from 25 to the character count of your need. You would have to estimate the count for your theme design. The code adds the length variable and then use the conditional tag to see if the title length matches our desired length. If it is longer, then the code adds ‘…’ in front. Most of the time, you will use this only on specific areas where the width is fixed in the theme.

*This is a good code to have as a theme designer*.

Source: Codezroz

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
dustinporchia 5 pts

This is golden!....Thanks wpbeginner!

Sweet! was just looking for a clients project

Does anybody know how the link title of previous_post_link(); could be truncated?

thanks

Thanks this worked perfectly! I didn't want to end up using a plugin just to do this and was happy your code cut down the titles properly.

Thanks again!

Hey there - thanks for writing this up! I happened to stumble upon another example of how to achieve this and it seemed a lot more simple, so I will share it with you and see what you think.

functions.php:

function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

These are two entirely different concepts.... The one you recommend is for post excerpts whereas the one we are talking about is for Post Titles.

Wow, that's embarrassing. :)

Hi!
This doesn't work if you use other languages than english. My swedish titel looks awful because the code doesn't translate å ä and ö comparing to the default code. Any suggestions of what I have to do? This is a good trick and i want to use it.

Kindly Lillan

Hmm... that does sound like a serious issue. Wondering if you can specify the language via PHP, so it counts characters in that instead of english.

adm_mnz 5 pts

If you use mb_substr there is a parameter for encoding.

http://php.net/manual/en/function.mb-substr.php

Thanks for the Trackback!

We appreciate your work for the community. Thanks for the nice snippet :)

One Suggestion, this type of code should be included in functions.php and not index.php. You can use conditional tags to restrict the code to any page you want whether its the homepage or any other page where you want.

This is just for specific areas... But yes, it can be customized and placed in functions.php

Don’t use strlen(). Use mb_strlen() or strlen(utf8_decode($str)) or you risk to truncate the string inside of a multi-byte character. The same applies to mb_substr().

Oh, and an ellipsis is one character: … :)

snipsley 5 pts

Thanks!! mb_strlen() a mb_substr solved my encoding problem. I've been looking for this for hours!

Lena Backstedt 5 pts

BIG tnx!

 

mb_strlen() also seems to work for the swedish language (so far I can see)

Tweets about us: