How to Truncate Post Titles in WordPress

Home » Snippets » How to Truncate Post Titles in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you finding your WordPress blog page cluttered due to excessively long post titles? Truncating post titles can be a handy solution to keep your website’s design sharp and ensure it is not overwhelmed by unwieldy text blocks. The process of doing so may seem challenging, but with this article, we provide an easy, step-by-step guide on how to truncate post titles on your WordPress platform.

					function wpturbo_truncate_post_title( $title, $length = 50, $end = '...' ) {
    if ( strlen( $title ) > $length ) {
        $truncated = substr( $title, 0, $length - strlen( $end ) ) . $end;
        return $truncated;
    }
    return $title;
}
add_filter( 'the_title', 'wpturbo_truncate_post_title', 10, 2 );
				

The main purpose of this code snippet is to truncate or shorten the post title in WordPress and append an ending string to it.

The code begins by defining a function called wpturbo_truncate_post_title(). This function is meant to shorten the post title. It takes three parameters: $title, $length, and $end.

function wpturbo_truncate_post_title( $title, $length = 50, $end = '...' )

The $title parameter represents the original post title that will be truncated, $length is the desired maximum length of the truncated title (default is 50), and $end is the string that will be attached to the end of the truncated title (default is ...).

In the body of the function, we first check whether the length of the title is greater than the desired maximum title length:

if ( strlen( $title ) > $length )

If it is, the code goes into the body of the if statement and uses the substr function to truncate the title.

$truncated = substr( $title, 0, $length - strlen( $end ) ) . $end;

Here, substr cuts the title at the specified length minus the length of the $end string, to make sure the final string still stays within the allowed length. Then it appends the $end string to the truncated title.

If the original title length is not greater than the specified length, the function simply returns the original title:

return $title;

This ensures that if the title is shorter than the maximum permitted length, it won’t be unnecessarily truncated.

Lastly, the wpturbo_truncate_post_title function is added to the WordPress the_title filter hook:

add_filter( 'the_title', 'wpturbo_truncate_post_title', 10, 2 );

The the_title filter hook allows developers to modify the post title before it’s displayed. The 10 after the function name is the priority of the function and the 2 is the number of arguments accepted by the function. Now, every time the title of the post is being called, our custom function wpturbo_truncate_post_title() will shorten it, if required.

Register an account to save your snippets or go Pro to get more features.