Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Truncate WordPress Post Titles with PHP

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
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() ?>">
$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 "...";

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

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Achintha says:

    Hey There is a wordpress function for limit words. Better to use that.

  2. dustinporchia says:

    This is golden!….Thanks wpbeginner!

  3. adm_mnz says:

    If you use mb_substr there is a parameter for encoding.

  4. Junaid says:

    Sweet! was just looking for a clients project

  5. Marco says:

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


  6. Brow says:

    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!

  7. Joey Figaro says:

    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.


    function new_excerpt_length($length) {
    return 100;
    add_filter(‘excerpt_length’, ‘new_excerpt_length’);

  8. Lena says:

    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

    • Editorial Staff says:

      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.

      • Alex says:

        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 :)

  9. Ben Kulbertis says:

    Thanks for the Trackback!

  10. Navjot Singh says:

    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.

    • Editorial Staff says:

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

  11. Thomas Scholz says:

    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 says:

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

    • Lena Backstedt says:

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

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.