Dynamic post the_title() length in WordPress

wordpress-featured

If you want to be able to limit the result returned when you display the title, first add this code to your themes functions.php file:

function the_title_dynamic($titlelength) { // Outputs a title of variable length (in characters)
   global $post;
   $text = $post->post_title;
   if (== $text ) {
      $text = get_the_title();
   }
   $output = strlen($text);
   if($output <= $titlelength) {
     $text = substr($text,0,$titlelength);
   }else {
      $text = substr($text,0,$titlelength).'…';
   }
   echo $text;
}

Call in loop:

the_title_dynamic(50)

 



Leave a Comment

You must be logged in to post a comment.