Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Automatically Truncate Blog Post Titles in WordPress

Do you want to learn how to truncate post titles with PHP?

Truncating, or shortening, lets you control the length of your blog post titles across your website. Depending on your WordPress theme, you may want to display shorter titles than what your theme supports.

In this article, we will show you how to truncate post titles in WordPress.

How to Automatically Truncate Blog Post Titles in WordPress

Why Truncate Post Titles in WordPress With PHP?

Truncating post titles in WordPress with PHP gives you more control over the length of your post titles and how they display on your website.

For example, you might want to cut off long post titles on your homepage so they don’t throw off the design of your WordPress blog.

Truncate posts example

Note: Some users simply want to use shorter post titles to optimize blog posts for SEO. In this case, you don’t need to truncate post titles. Instead, you can simply use a WordPress SEO plugin to make your title tag shorter.

An SEO plugin will let you create custom SEO titles for the search result pages while still keeping longer post titles for your visitors on your site.

For more details, see our ultimate guide on how to set up All in One SEO correctly.

With that said, let’s show you how to truncate WordPress post titles on your website by using two different methods:

Method 1: Truncate WordPress Post Titles With a WordPress Function

The easiest way to truncate WordPress post titles in WordPress is by adding PHP code to your WordPress files. If you haven’t done this before, then check out our guide on how to copy and paste code in WordPress.

Many tutorials will tell you to add code directly to your theme’s functions.php file. However, any mistakes could cause a range of WordPress errors or even break your site.

That’s why we recommend using the free WPCode plugin instead, following our guide on how to add custom code in WordPress.

First, you need to install and activate the WPCode free plugin. If you need help with this, then please see our step-by-step guide on how to install a WordPress plugin.

Upon activation, select Code Snippets » + Add Snippet from your admin sidebar. Next, hover your mouse over the ‘Add Your Custom Code (New Snippet)’ option and then click the ‘Use snippet’ button that appears.

Adding Custom Code in WPCode

This will open a new page where you can type a title for the snippet and then add the code.

Simply copy and paste the following code into the Code Preview pane in WPCode.

function max_title_length( $title ) {
$max = 35;
if( strlen( $title ) > $max ) {
return substr( $title, 0, $max ). " …";
} else {
return $title;
}
}

add_filter( 'the_title', 'max_title_length');
Truncating Headlines Using WPCode

This code will execute inside your WordPress post loop and shorten your blog post titles to ’35’ characters. To change the length of your title, just set the $max variable to your preferred title length.

Next, you need to choose ‘PHP Snippet’ from the Code Type drop-down menu.

Finally, you’ll need to toggle the Active setting on and then save the snippet by clicking the ‘Save Snippet’ button.

Truncating Headlines Using WPCode

Now that the code snippet is active, your blog post titles will be shortened wherever they appear on your WordPress website.

Method 2: Truncate WordPress Post Titles With PHP by Changing WordPress Theme Files

Another way to truncate WordPress post titles is by adding code directly to your WordPress theme files.

This method gives you more control over where your titles are shortened. For example, you might want to cut off titles on your homepage only but display the full-length title on the blog post.

To do that, you will need to add the PHP code directly to the WordPress theme files where you want to truncate your blog post titles.

For example, you can add the code snippet below to your index.php file to replace the existing the_title tag inside your WordPress post loop to change the title length sitewide:

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

This code sets the length of the post title to 25 characters. If the length of the title is longer than 25 characters, then it will cut off the title after 25 characters and add an ellipsis ‘…’ to the end.

To change the character length for your website, simply change the $thelength variable to your preferred character count.

Once you have added the code and saved your file, you need to upload it to your theme directory in your WordPress hosting account.

You can do this by using an FTP client or the file manager tool in your WordPress hosting control panel. If you haven’t used FTP before, then check out our guide on how to use FTP to upload files to WordPress.

After the code is added, your post titles will be truncated to the character count you set.

Pro Tip: If you used Method 2, then you will lose these changes when you update your theme to a new version. To avoid this, see our guide on how to update your WordPress theme without losing customization.

We hope this article helped you learn how to truncate WordPress post titles with PHP. You may also want to see our guide on how to write a great blog post and our expert picks of the best schema markup plugins for WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

21 CommentsLeave a Reply

  1. kzain

    A great solution for cleaning up those long titles!
    Especially with catchy headlines, sometimes they can get a bit out of hand. This custom function seems straightforward to implement, although I might need a friend to help with the PHP part.

  2. dustinporchia

    This is golden!….Thanks wpbeginner!

  3. Junaid

    Sweet! was just looking for a clients project

  4. Marco

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

    thanks

  5. Brow

    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!

  6. Joey Figaro

    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’);

    • Editorial Staff

      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.

      Admin

  7. Lena

    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

    • Editorial Staff

      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.

      Admin

      • Alex

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

    • Editorial Staff

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

      Admin

  8. Navjot Singh

    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

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

      Admin

  9. Thomas Scholz

    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

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

    • Lena Backstedt

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

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.