How to Recduce the Character Length of WordPress Post Title

I want to limit the characters of post title in WordPress.

Suppose I have published an article with 150 characters post title but I want to show only 120 characters. Is there any way to do it?

Related posts

Leave a Reply

3 comments

  1. You could simply edit the header of your theme in wp-content/themes/your-theme/header.php. Then change the title depending on the number of characters:

    <title><?php
        $title = wp_title(' - ', false, 'right');
        if (strlen($title) > 120) $title = substr($title, 0, 117) . "...";
        echo $title;
    ?></title>
    
  2. <a href="<?php echo $blogUrl; ?>?p=<?php echo the_ID(); ?>">
    <?php 
        if (strlen($post->post_title) > 120) {
            echo substr(the_title($before = '', $after = '', FALSE), 0, 120) . '...'; 
        } else {
            the_title();
        } 
    ?>
    </a>