Skip to main content
added 38 characters in body; edited tags
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

So I'm using this filter in order to shorten the title:

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

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;
}
}

and in my template:

 <?php the_titlesmall('', '...', true, '50') ?>

This works like a charm.

However, this filter adds "..." to every post title, even if the post title is under "50" characters and it looks weird. How can I only add the "..." to the titles that are over "50" characters?

Thanks a lot!

So I'm using this filter in order to shorten the title:

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;
}
}

and in my template:

 <?php the_titlesmall('', '...', true, '50') ?>

This works like a charm.

However, this filter adds "..." to every post title, even if the post title is under "50" characters and it looks weird. How can I only add the "..." to the titles that are over "50" characters?

Thanks a lot!

So I'm using this filter in order to shorten the title:

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;
}
}

and in my template:

 <?php the_titlesmall('', '...', true, '50') ?>

This works like a charm.

However, this filter adds "..." to every post title, even if the post title is under "50" characters and it looks weird. How can I only add the "..." to the titles that are over "50" characters?

Thanks a lot!

Source Link
Johann
  • 867
  • 4
  • 14
  • 31

Shorten the title length

So I'm using this filter in order to shorten the title:

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;
}
}

and in my template:

 <?php the_titlesmall('', '...', true, '50') ?>

This works like a charm.

However, this filter adds "..." to every post title, even if the post title is under "50" characters and it looks weird. How can I only add the "..." to the titles that are over "50" characters?

Thanks a lot!