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’ll 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 setup 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 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.

You can add the code snippet below to your functions.php file, in a site-specific plugin, or by using a code snippets plugin.

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 set the $max variable to your preferred title length.

Once you’ve added one of 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 only cut off titles on your homepage, 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 title length site wide.


post_title; /* or you can use get_the_title() */
$getlength = strlen($thetitle);
$thelength = 25;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo "...";
?>

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 ‘…’ to the end.

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

Once you’ve 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 with be truncated to the character count you set.

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 choose the best web design software and our expert picks of the best free website hosting.

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.