Let’s say you’ve written lots of posts with lengthy titles. But now you’re switching to a theme that doesn’t handle long titles well. Copy this into a file, name it truncateTitle.php (or whatever.php), upload to your plugins directory, and activate it in the usual way. Don’t leave any blank lines or spaces before or after this code (or you’ll get an error).
<?php
/*
Plugin Name: KB Truncate Title
Plugin URI: http://adambrown.info/b/widgets/category/custom/
Description: Truncates titles so they don't break your theme
Version: 1.0
Author: Adam R. Brown
Author URI: http://adambrown.info/
*/
define('KB_TRUNCATE_TITLE', 50); // change 50 to max number of characters to allow
function kb_truncateTitle($title){
if (!is_int( KB_TRUNCATE_TITLE ) || 1>KB_TRUNCATE_TITLE )
return $title;
// rather than just use substr, let's make sure we cut it at the end of a word
$arr = explode(' ', $title);
$out = '';
$count = 0;
foreach( $arr as $str ){
$count += ( strlen($str) + 1); // +1 is for the space we removed
if ($count > KB_TRUNCATE_TITLE)
break;
$out .= $str . ' ';
}
// make sure we got SOMEthing
if (!$out)
$out = $arr[0];
return $out;
}
// makes the function work
add_filter('the_title','kb_truncateTitle');
?>
6 comments »
I’m wondering what WordPress “titles” your script actually alters. For example, I’m looking for a script that only alters the top WordPress navigation titles of each post, as seen here:
Let Loose Upon Lollapalooza, Day 1
[link]
In the above example page, I’d like to take the navigation title “Let Loose Upon Lollapalooza, Day 3″ (top, right) and make it (and all titles longer than 20 char’s) get cut-off at 20 char’s with “…” placed afterwards, like so (without the quotes, those are for descriptive purposes):
“Let Loose Upon Lolla…”
Is this what your script does… or does it also truncate titles other than in the navigation?
Ahh, that’s what you’re asking. I read your earlier email too quickly. The best answer to your question is “try it and see,” since things don’t always work as intended. But it’s not intended to work the way you’re asking–it’s intended to truncate the titles when they occur at the beginning of each post on your blog. Note this line at the end of the code:
That’s what triggers the plugin. When your blog’s theme calls
the_title(), thethe_titlehook gets activated, and my code runs. I don’t know whether that hook also gets called by the navigation links, but it does get called within the loop.Hey there,
I’ll give it a shot when I’m back from traveling at the end of the month.
Thanks!
Hi,
Is there anyway to get this plugin to only truncate the titles displayed in the sidebars?
Example:
how do I trun…(L sidebar) How do I truncate titles? (actual post)
Sean, that depends on how the titles are showing up in your sidebar. You can make the truncate function apply to anything you want, if there’s a filter hook for it. See my comment above about how the plugin uses the
the_titlehook.You can find a complete list of WP hooks here: WP hooks database. Maybe you can find one there that will work for you.
Hi Adam,
THX! but darn it! Almost blown a fuse trying to get it to work the way I want - suffered even more frustration looking at the tables - oh my! The the_title hook truncates all post titles in both sidebars and main page. I just want it to truncate titles in recent posts (which rests in the sidebar) - to stop the text wrapping and spoiling my lovely rollover menu). Guess I’ll take a break from it before I accidentally put my fist through the monitor - heheh
Cheers anyway.
Leave a comment