How to Limit the Title Length in WordPress?

Limiting the post title is very important when you are running a multi-author WordPress blog.

There are many plugins available to limit title length in WordPress but they would to it sitewide.

Here we are giving small code snippets that you can apply on a particular page instead of the entire side.

If you are a little bit familiar with WordPress file structure and theme code you can do it easily.

First, locate functions.php file in your active WordPress theme and add the given function in the file.

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

The second step is to call the function on the template where you want the title limit.

Here is the code snippet you can use on a page template, post template or any other template of your theme.

<?php wpclap_short_title('...', 45); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *