How to limit the max number of characteres in the title that are displayed

I am following this tutorial — “Display Your Popular Posts In WordPress Without A Plugin” and want to limit the max number of characters in the title that are displayed. Also, why is the thumbnail, at times, smaller than the number I define in the php?

3 Answers
3

first add this function to your functions.php file

function max_title_length($title){
     $max = 20;
    return substr( $title, 0, $max ). " …";
}

then before the loop of the code you linked add this line to hook the above function:

add_filter( 'the_title', 'max_title_length');

and after the loop remove this filter:

remove_filter( 'the_title', 'max_title_length');

and just change $max = 20; to whatever you want.

Leave a Comment