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
Dear Staff,
I need your help as I want Truncate WordPress Post Titles with PHP script for the thumbnails and post sliders only.
Can you please help me to do this. I tried to use provided script in wordpress index.php but it is not working then I added this script in theme index.php but the result is remained the same.
I will be thankful if you please provide me full script to get the desired results.
my website which is under construction is anews.pk
Regards.
Hey There is a wordpress function for limit words. Better to use that.
http://codex.wordpress.org/Function_Reference/wp_trim_words
This is golden!….Thanks wpbeginner!
If you use mb_substr there is a parameter for encoding.
http://php.net/manual/en/function.mb-substr.php
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.
You might wanna try to specify a different charset, check Latin1 or utf8 i think they contain those chars as well.. I had some similar issues recently since my first language is german
Thank you for helping out Alex
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: …
Thanks!! mb_strlen() a mb_substr solved my encoding problem. I’ve been looking for this for hours!
BIG tnx!
mb_strlen() also seems to work for the swedish language (so far I can see)