Hi Guys, I've been struggling with how to limit the length of a post title in wordpress..? I've got as far as it will limit what is displayed on the screen, but the problem is it is not updating or changing the original length of the title, Which is a major problem if I submit a post to Twitter for example. The code: 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; } PHP: Does anyone know how to get the_title in wordpress to update to the truncated version of the title that gets displayed on the screen..? Thanks Steve
function the_title_shorten($len,$rep='...') { global $wpdb; $title = the_title('','',false); $shortened_title = textLimit($title, $len, $rep); if($title != $shortened_title) { $sql = 'UPDATE ' . $wpdb->prefix . 'posts ' SET post_title = ' . $shortened_title . ' WHERE ID = ' . the_ID(); $wpdb->get_results($sql); } print $shortened_title; } Code (markup): I hope this one helps.
@ drctaccess, No I'm sorry it did not help, all it did was create a 500 Server Error ! Good job I used a test blog to try it out eh..? Otherwise it would have trashed my site ! Ulike many errors, where if you put the code back as it was, you can often cure the problem, this was unrecoverable. But no worries I'll just completely overwrite the DB & be back to square one in 5 mins Thanks for trying anyway
Sorry drctaccess, Site has already been reinstalled with new wordpress & database, so no log files available unfortunately.