• 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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Try:

    function truncate_title( $title ) {
    	$title = textLimit($title, $len, $rep);
    	return $title;
    }
    add_filter( 'the_title', 'truncate_title' );
    
    //shorten without cutting full words
    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;
    }

    Thread Starter c3lt1957

    (@c3lt1957)

    Hi esmi,

    Thanks for this, but could you tell me how I would call the function..?

    Sorry but my knowledge of PHP is bordering on non existant!

    Thanks Steve

    You don’t need to call the function. The add_filter( 'the_title', 'truncate_title' ); line ensures that it is run automatically with every call to the_title.

    Thread Starter c3lt1957

    (@c3lt1957)

    Hmm ok Esmi,

    one other thing though & I appologise if this sounds stupid but php is not my strong point.

    On the example code I showed above, the variable $len & $rep are assigned their values in the function call for that piece of code eg.
    <?php the_title_shorten(80,'...'); ?>

    Now if I replace the code I am using with the code you have suggested, I assume that function call will no longer work and will need to be changed back to; <?php the_title(); ?> ..? If this is the case, then I have to ask where does $len & $rep get their values..?

    Thanks Steve

    Where were you getting the values for these variables from in the first place? You could always assign them in the first function:

    function truncate_title( $title ) {
    	$title = textLimit($title, 40, '&hellip;');
    	return $title;
    }
    Thread Starter c3lt1957

    (@c3lt1957)

    I changed <?php the_title(); ?> in my main index.php to;
    <?php the_title_shorten(80,'...'); ?>
    So the values were assigned each time it ran through the loop.

    Ok thanks, I’ve got it now (I hope)
    So will this new code you suggest actually update and change the titles to the truncated versions permenantly..? or only prior to display on the screen..?

    I have to ask because I need to change the title to the truncated versions permenantly, so that I am then able to bookmark my posts / articles without having to go through each one to trim it to length.

    Thanks Steve

    Only prior to display on the screen. The original titles will remain intact in the database. If you want to use a permanent “shortlink”, the best method is to use the post’s default permalink – eg: http://mysite.com/?p=123

    That shouldn’t ever break.

    Thread Starter c3lt1957

    (@c3lt1957)

    Hello esmi,

    Surely post title & permalink are two completely different things..?

    It is not the URL I need to shorten, I can do that easy enough using bit.ly.
    I may not know much about php or mysql, but I am damned sure there must be a method to assign a new value to an already created entry, so if “the_title” or “$title” is the one I need to change & “$string” is the new value (the truncated title), do you have any idea what the correct syntax would be to assign this new value..?

    Thanks Steve

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Help with "the_title" in WordPress pls’ is closed to new replies.