1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Can anyone pls tell me how to update the_title after title is truncated

Discussion in 'PHP' started by Taffy1957, Jan 24, 2011.

  1. #1
    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
     
    Taffy1957, Jan 24, 2011 IP
  2. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #2
    
    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, Jan 25, 2011 IP
  3. Taffy1957

    Taffy1957 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    @ 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
     
    Taffy1957, Jan 25, 2011 IP
  4. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Can you look in the error log and provide the error (not just the number) ?
     
    drctaccess, Jan 25, 2011 IP
  5. Taffy1957

    Taffy1957 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry drctaccess,

    Site has already been reinstalled with new wordpress & database, so no log files available unfortunately.
     
    Taffy1957, Jan 25, 2011 IP