Removing some characters in the title is not necessary and I won’t recommend doing so as it will hurt your website’s SEO.

However, there are instances that we need to strip out and minimize the length of the title, especially if we are concern with layouts and appearance.

Very long title would break your website’s layout especially if you are making a grid type post preview, with some post having very long titles, and some with shorts which is a nightmare for designers.

To achieve this, we will use a PHP function to strip out some characters. I created a function below which you can use or as your reference of what we are trying to achieve.

The Function

Just copy the code below and paste it into your theme’s functions.php

function ywt_limit_chars( $string, $char_limit, $ext = null ){
    $extCount = 0;
    $charCount = strlen($string);
    if($ext != null) {
        $extCount = strlen($ext);
    }
    if($charCount > $char_limit){
        return substr($string, 0, $char_limit-$extCount) . $ext;
    }else{
        return substr($string, 0, $char_limit);
    }
}

Parameters

The function above accepts 3 parameters

$string = (required) (string) The string or the whole title
$char_limit = (required) (int) The number of characters to retain. From left to right.
$ext = (optional) (string) The text appended after the shopped title example ‘…’

How to Use

To use the function, simply call the function within your theme’s template file with supplied parameters. Example, lets limit the post title to print 15 characters only and append “…” if it contains more than 15 characters:

<?php
if(have_posts()) :
    while(have_posts()) : the_post();

        echo ywt_limit_chars(get_the_title(), 15, '...');

    endwhile; 
endif; 
?>