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

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

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 truncate WordPress post titles with PHP (2 ways)

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. Simply follow our guide on how to add custom code in WordPress, and add the following code to your WordPress site:

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

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.

Once you have added the code snippet above, 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. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. 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!

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

    functions.php:

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

  4. Lena says

    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

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

  6. 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: … :)

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.

WPBeginner Assistant
How can I help you?

By chatting, you consent to this chat being stored according to our privacy policy and your email will be added to receive weekly WordPress tutorials from WPBeginner.