[Support request] truncate title too long

Home Forums Support truncate title too long

  • This topic has 1 reply, 2 voices, and was last updated 1 year ago by David.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1195992
    anand

    how to cut titles which are too long, say more than 60 characters and append … trailing to that title

    #1196498
    David
    Staff
    Customer Support

    Hi there,

    it means filtering the_title function, which is a bit of a pain as that function can be used in more places then just the post title. But you can try this PHP snippet:

    function archive_title_length( $title ) {
        $max = 60;
        if( strlen( $title ) > $max && !is_admin() && !is_single() ) {
            return substr( $title, 0, $max ). " …";
        } else {
            return $title;
        }
    }
    
    add_filter( 'the_title', 'archive_title_length');

    This should only apply to frontend archives.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.