[resolved] How do I truncate titles? (5 posts)

  1. FofR
    Posted 10 years ago #

    Titles are often too large for my layout and I'd like to make them smaller. The title output is given by "the_title( )" which I can't truncate using the usual PHP function and I can't seem to pass the contents of the_title to another string and then truncate that.

    Any help?

  2. Les Bessant
    keeper of the Tiggers
    Posted 10 years ago #

    How about get_the_title()? That should return the value rather than echo it...

  3. Austin Matzko
    Posted 10 years ago #

    There's a filter hook for titles called 'the_title'. That means all you need is to adapt a truncating PHP function, like the one here: http://www.the-art-of-web.com/php/truncate/
    and add it to the filter, like so:


  4. jbbrwcky
    Posted 10 years ago #

    Try this code to truncate to the length you want:

    $tit = the_title('','',FALSE);
    echo substr($tit, 0, 51);
    if (strlen($tit) > 51) echo " ...";

    Change the numbers 51 to whatever length you want.

  5. FofR
    Posted 10 years ago #

    Thank you, a combination of get_the_title() and jbbrwcky's solution worked.

Topic Closed

This topic has been closed to new replies.

About this Topic