Ready to get started?Download WordPress


Post title char count? (5 posts)

  1. Vast HTML
    Posted 5 years ago #

    Is ther a way to limit the number of letters in the post title. on my index.php it looks so bad when the title takes up 2 lines so can it be cut off at a certain point?

  2. Len
    Posted 5 years ago #

    heh. I ran into this same problem when I redesigned my front page. Long post titles wrapped to the second line causing that content block to drop out of alignment with respect to the others.

    The fix is simple enough - create a new function then call it in whatever template file you want.

    Add this to your theme's functions.php ...

    function trim_title() {
    $title = get_the_title();
    $limit = "x";
    if(strlen($title) <= $limit) {
    echo $title;
    } else {
    $title = substr($title, 0, $limit) . $pad;
    echo $title;

    Adjust the line $limit = "x"; to whatever you want. (say 20 for example)

    Then look in the appropriate template file for the_title() and replace with your new function trim_title() If your post title is more than the limit you specified it will trim it to that specification. If it is less it will do nothing.

  3. Vast HTML
    Posted 5 years ago #

    Thanks a ton :)

  4. bekar09
    Posted 5 years ago #

    thats a perfect solution. thanks a lot.

  5. ahsanaltaf
    Posted 4 years ago #

    Great exactly what i was looking for and had the same problem as LenK.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.