How do I show ‘…’ when the WordPress title exceeds length

I have a function which trims the title to a certain length. It does trim the title properly, but always shows ‘…’ even if the title doesn’t exceed the length.

Does anyone know how to make it so the ‘…’ will only show if the title is too long?

// Title Excerpt//
function the_titlesmall($before = '', $after = '', $echo = true, $length = false) { $title = get_the_title();

    if ( $length && is_numeric($length) ) {
        $title = substr( $title, 0, $length );
    }

    if ( strlen($title)> 0 ) {
        $title = apply_filters('the_titlesmall', $before . $title . $after, $before, $after);
        if ( $echo )
            echo $title;
        else
            return $title;
    }
}
//End Title Excerpt

PHP in loop

<?php the_titlesmall('', '', true, '15') ?>

Read more here: How do I show ‘…’ when the WordPress title exceeds length

Leave a Reply

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