truncate WordPress post titles with php og

Do you want to understand how to use PHP to truncate post titles?

Advertisements

You may regulate the length of your blog post titles across your website by truncating, or shortening, them. You may want to display shorter titles than what your WordPress theme offers, depending on your theme.

We’ll show you how to truncate post titles in WordPress in this article.

How to truncate WordPress post titles with PHP (2 ways)

Why use PHP to truncate post titles in WordPress?

Using PHP to truncate post names in WordPress provides you more control over the length of your titles and how they appear on your site.

On your homepage, for example, you might want to trim off long article names so that your WordPress blog’s design isn’t thrown off.

Truncate posts example

Note: Some users merely want to optimize blog posts for SEO by using shorter post names. You don’t need to truncate post titles in this scenario. To make your title tag shorter, you may simply utilize a WordPress SEO plugin.

An SEO plugin will allow you to build bespoke SEO titles for search result pages while also allowing you to preserve longer post titles for your site’s users.

See our complete guide to setting up All in One SEO appropriately for more information.

With that in mind, let’s look at two distinct approaches for truncating WordPress post names on your website.

Method 1: Using a WordPress Function to Truncate WordPress Post Titles

Adding code to your WordPress files is the simplest technique to truncate WordPress post names. Check out our instruction on how to copy and paste code in WordPress if you haven’t done it before.

The code snippet below can be placed in your functions.php file, a site-specific plugin, or a code snippets plugin.

1
2
3
4
5
6
7
8
9
10
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 condense your blog post titles to ’35’ characters and run inside your WordPress post loop. Set the $max variable to your preferred title length to adjust the length of your title.

Advertisements

Your blog post titles will be abbreviated anywhere they appear on your WordPress website once you’ve included one of the code snippets above.

Method 2: Change WordPress Theme Files to Truncate WordPress Post Titles with PHP

Another option is to add code straight to your WordPress theme files to shorten post titles.

You have more control over where your titles are abbreviated with this strategy. For example, you might only want to trim off headlines on your homepage but keep the complete title on your blog article.

You’ll need to add the PHP code straight to the WordPress theme files where you wish to truncate your blog post names in order to accomplish this.

For example, to modify title length site-wide, add the code snippet below to your index.php file to replace the existing the title element inside your WordPress post loop.

1
2
3
4
5
6
7
8
9
<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>

 

The post title will be 25 characters long if this code is used. If the title is longer than 25 characters, it will be cut off after 25 characters and a ‘…’ will be appended at the end.

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

After you’ve finished adding the code and saving the file, upload it to your WordPress hosting account’s theme directory.

You can do this with an FTP client or your WordPress hosting control panel’s file manager.

Check out our guide on how to utilize FTP to upload files to WordPress if you’ve never done so before.

Your post titles will be reduced to the character count you specify after the code is inserted.

We hope this tutorial taught you how to use PHP to truncate WordPress post titles. You might also be interested in our guide to selecting the best web design software and our expert recommendations for the best free website hosting.

 

Advertisements

Leave a Reply