Limit Title plugin

I hesitate to call this a “plugin”, given that it only contains 5 lines of active code, but that’s exactly what it is: a plugin. What does it do? It limits the display length of Post and Page titles – without truncating words.

Many people like to use magazine style themes for their WordPress site, but often the gorgeous, lovingly created home page layout can be spoiled by a post title which wraps onto a second line, which breaks the layout and upsets the look of the page.

This seems to be more often a problem with magazine style layouts because they usually contain multiple columns, all relatively narrow, in which to display the home page content. Now, the simple fix, to avoid this issue in the first place, is to be careful when writing your Post title to make sure that it doesn’t contain too many characters. But sometimes this isn’t possible. What if you really must have a post called “The absolutely longest post title in the history of the world, possibly ever!”, which won’t fit in the box on your homepage, but is perfect when viewed in the single post page? Easy. Install my Limit Title plugin and you will be able to truncate the title length to suit your layout!

Limit Title plugin download

If you just want to use it and aren’t interested in how it works, you can simply download it here:

This download is no longer available.

How it works

This is the code that does the magic:

function the_title_limit($length, $replacer = '...') {

$string = the_title('','',FALSE);
       
if(strlen($string) > $length)
$string = (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;

echo $string;
}

The function takes two arguments:

  • $string = number of characters to display
  • $replacer = string to be displayed at the end of the truncated title. The default is “…” (without the quotes).

Use it in functions.php instead

If you prefer, you can simply add the function code directly to functions.php without bothering installing it as a plugin. Add this to your functions.php file:

/* the_title_limit() New template tag
By Ade Walker, http://www.studiograsshopper.ch

Use it in theme files in place of the_title(); to limit the length of the Post or Page title

Function: the_title_limit( $length, $replacer);

Arguments:
	$length = number of characters to display (required)
	$replacer = string to display at the end of the shortened title to show that title has been shortened

Usage example:
	the_title_limit( 15, '...' )
	
*/
function the_title_limit($length, $replacer = '...') {

$string = the_title('','',FALSE);
       
if(strlen($string) > $length)
$string = (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;

echo $string;
}

Enjoy!

Comments

  1. Thanks Plugin ! But, the Vietnamese did not show well, with no way ? Example: Điểm G cực khoái chỉ là tư�…

  2. Hi, Wish you overcome this error. Thanks

  3. Do I have to configure this plugin in any form?

    I tried to add the code to my functions.php, didn’t do anything…

    Then I installed the plugin and again, nothing…

    How can I make it work?

    • Brad E. says:

      I just added the function to the bottom of my functions.php file in my theme folder and it worked fine. be sure you add the function at the very end of that file and the function is in PHP tags. Also, be sure you are editing the correct theme folder

  4. Hi,

    I tried to download and your dl link is not working for me in firefox..
    Tried to find it in wordpress directory but not sure if I fount the right one…

    Can you post the link please?

    Thanks

    Mike

  5. Thank you again for the codes.
    Yep the download link is not working but you can just copy and paste the code in your functions.php file.
    Remember to first backup your functions.php

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*


eight − = 7