• Hello everybody.
    Does anybody by coincidence know of a way to automatically trim titles to x amount of words or letters? I´d really appreciate. It is for a menu, where super-long titles don´t go very well..
    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • A little PHP that strips the title down by character count:

    <?php
    $short_title = the_title('','',false);
    $short_title = substr($short_title,0,30);
    echo $short_title;
    ?>

    Change the last value in substr() (30) to the number of characters you only want to display. Also, if you need your abbreviated titles to be links, pull the echo line out and and insert it as a separate *tag* where you need it:

    <?php echo $short_title ?>

    Here’s the PHP manual entry for substr, I always forget how it works:

    http://php.net/substr

    Thread Starter Wybe

    (@wybe)

    thanks!!!! omg man you are my hero.
    i never knew that kind of thing was so easy in PHP. but then again, I dont know much of it 🙂 thanks thanks!

    and with my limited knowledge i have made it

    $short_title = the_title("","",false);
    $short_title_2 = substr($short_title,0,20);
    echo $short_title_2;
    if($short_title_2!=$short_title) { echo "..."; }

    it will append three dots when the title has been shortened. if not, the complete title is ofcourse displayed 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to trim the title of a post?’ is closed to new replies.