Hey guys, I've been trying to get my post titles truncated because some of the titles pulled in from Amazon are of a ridiculous length & would very often prevent that post from being submitted to the likes of Twitter.
The code I used;
//function to call and print shortened post title
function the_title_shorten($len,$rep='...') {
$title = the_title('','',false);
$shortened_title = textLimit($title, $len, $rep);
print $shortened_title;
}
//shorten without cutting full words (Thank You Serzh 'http://us2.php.net/manual/en/function.substr.php#83585')
function textLimit($string, $length, $replacer) {
if(strlen($string) > $length)
return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;
return $string;
}
This code works fine as far as displaying the_title on the screen, but unfortunately it does not update the_title to the truncated version, which is really what I need it to do for the reason mentioned above.
I'd very grateful if anyone can offer some advice on how to acheive this..?
Thanks Steve