WordPress.org

Ready to get started?Download WordPress

Forums

How to trim the title of a post? (4 posts)

  1. Wybe
    Member
    Posted 9 years ago #

    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!

  2. Kafkaesqui

    Posted 9 years ago #

    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 ?>

  3. Matt Mullenweg
    Troublemaker
    Posted 9 years ago #

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

    http://php.net/substr

  4. Wybe
    Member
    Posted 9 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.