WPGeek

How to limit WordPress posts title

During my work as a wordpress theme coder I came across that there are request for a function on how to limit the post titles. So here you are the code and hope that it will be useful for you:

Open functions.php in your theme and copy/paste this code:

<?php function limit_title($title, $n){
if ( strlen ($title) > $n )
{
echo substr(the_title($before = ”, $after = ”, FALSE), 0, $n) . ‘…’;
}
else { the_title(); }
}
?>

Then just put this function where you want in your wordpress theme templates:

<?php limit_title($post->post_title, 30); ?>

Note:

  • Number in the gaps means to how much symbols you want to limit
  • The function should be used in the post loop
On Jul 31, 2009 Posted by WPGeek and it is filed under: Tutorials

Leave a reply


My Recent Works

SteveTenpenny.com
CopyKat.com
Community Living Durham North
BeautyIdol.com
Footballistics.com

Statistics of My Services in Use