Limit Title plugin

A perfectly useable Wordpress plugin to limit the display length of Post and Page Titles. Activate it, replace the_title() with the_title_limit(args) in your theme template files, wherever needed, and no more anguish when your long Post Titles break your theme layout.

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:

How it works

This is the code that does the magic:

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 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!

Filed under Plugin Lab

Comments

4 Responses to “Limit Title plugin”

  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. Carol says:

    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?

Leave a comment

Interesting article? Or a load of rubbish? Let me know...
and oh, if you want a pic to show with your comment, go get a gravatar!

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