How to limit the title length in WordPress?

While you are working on the client’s site that time design’s approach will not allow the long titles to be displayed. There are many plugins that would truncate WordPress titles or you can say it will limit the title length, but they would do it sitewide whereas we only wanted it on the particular page.

First, you need to add the given function in the functions.php

function short_title($after = '', $length) {
    $mytitle = get_the_title();
    if ( strlen($mytitle) > $length ) {
        $mytitle = substr($mytitle,0,$length);
        echo $mytitle . $after;
    } else {
        echo $mytitle;
    }
}

The second step is to call the function that you added in the functions.php where you need it.

Here is the example:

<?php short_title('...', 40); ?>

This will help you out!

Leave a Comment