WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. FofR
    Member
    Posted 8 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. How about get_the_title()? That should return the value rather than echo it...

  3. Austin Matzko
    Member
    Posted 8 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:

    add_filter('the_title','some_truncating_function');

  4. jbbrwcky
    Member
    Posted 8 years ago #

    Try this code to truncate to the length you want:

    <?php
    $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
    Member
    Posted 8 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

Tags