How to limit the length of titles using php ?

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

  1. #1
    Hi Guys,

    I've just set up a new Wordpress Blog & am having problems with the title length of some of the stuff I'm pulling in from Amazon as I also wish to feed these post titles & urls to Twitter (140 character limit).

    Anyway I've tried for a few days to modify the content_limit which is already part of my theme, to work as a title_limit, but unfortunately my php skills are only slightly better than useless!

    Ideally what I would like to achieve is a method of limiting the length of the titles, without breaking the permalinks which are in a h2 tag & also if possible truncating the title at the space between words, so at least it will still make partial sense.

    I'd be very grateful if anyone could offer me some advice on this as it's driving me nuts at the moment!

    Thanks Steve :confused:
     
    Taffy1957, Jan 19, 2011 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    use substr() function
     
    gapz101, Jan 19, 2011 IP
  3. solidsoul2010

    solidsoul2010 Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Can you supply some code your working with for me too look at please
     
    solidsoul2010, Jan 19, 2011 IP
  4. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #4
    Thorlax402, Jan 21, 2011 IP
  5. Taffy1957

    Taffy1957 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi Guys, thanks for the help & sorry for the delay in getting back to you, but I've been away for a couple of days working, anyway this is the code I am trying to sort out -

    <?php while (have_posts()) : the_post(); ?>
    
      <div id="archive">
    
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    
          <?php the_title(); ?>
    
          </a> </h2>
    
        <div class="left"><a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&amp;h=100&amp;w=100&amp;zc=1" alt="<?php the_title(); ?>" /></a></div>
    
        <div class="archiveright">
    
          <?php the_content_limit(400,''); ?>
    
        </div>
    
        <div class="clear"></div>
    PHP:
    It is the title within the H2 tags that is causing the problems & unfortunately I have no control over which products are selected for posting on my site, therefore as I already have a section of code in the functions.php which is limiting the content of each product description to 400 characters, I thought that perhgaps it would be possible to limit the post title in a similar way..??

    Thanks again Steve
     
    Taffy1957, Jan 21, 2011 IP
  6. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #6
    <?php while (have_posts()) : the_post(); ?>
    
      <div id="archive">
    
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    
          <?php echo substr(the_title(), 0, 140); ?>
    
          </a> </h2>
    
        <div class="left"><a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&amp;h=100&amp;w=100&amp;zc=1" alt="<?php the_title(); ?>" /></a></div>
    
        <div class="archiveright">
    
          <?php the_content_limit(400,''); ?>
    
        </div>
    
        <div class="clear"></div>
    PHP:
    this would limit it to 140 chars in title
     
    G3n3s!s, Jan 22, 2011 IP
  7. Taffy1957

    Taffy1957 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hey G3n3s!s, thanks man that actually worked !

    But there is one minor problem with it, it cuts the words if the limit falls
    in the middle of a word :confused:

    Is there a fairly easy way for me to correct this ?

    Thanks for your help at least I have something to work on now anyway.
     
    Taffy1957, Jan 23, 2011 IP
  8. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #8
    I am not sure if it will work but try it. It should type TITLEN.... if it has more than 140 chars.
    <?php while (have_posts()) : the_post(); ?>
    
      <div id="archive">
    
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    
          <?php 
    $s = substr(the_title(), 0, 140);
    if (strlen(the_title()) > 140) $dot = "...";  
    echo substr(the_title(), 0, 140).$dot; ?>
    
          </a> </h2>
    
        <div class="left"><a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&amp;h=100&amp;w=100&amp;zc=1" alt="<?php the_title(); ?>" /></a></div>
    
        <div class="archiveright">
    
          <?php the_content_limit(400,''); ?>
    
        </div>
    
        <div class="clear"></div>
    PHP:
     
    G3n3s!s, Jan 23, 2011 IP
  9. Taffy1957

    Taffy1957 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Fantastic,

    Your awesome dude !

    Thanks ;-)
     
    Taffy1957, Jan 23, 2011 IP
  10. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #10
    there is add reputation button
    forums.digitalpoint.com/reputation.php?do=addreputation&p=15664965

    ;)
    BTW, if you want to ask something do not hesitate to PM me/reply here
     
    G3n3s!s, Jan 23, 2011 IP
  11. Taffy1957

    Taffy1957 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Well Guys I got my test blog setup & tried out your suggestions, but unfortunately I couldn't get either method to work!
    However I have found another piece of code;
    <h2>
    
    	<a href="<?php the_permalink() ?>">  
    
    	<?php  
    
    	$thetitle = get_the_title(); 
    
    	$getlength = strlen($thetitle);  
    
    	$thelength = 80;  
    	
    	echo substr($thetitle, 0, strrpos( substr( $thetitle, 0, 80), ' ' ) );  
    
    	if ($getlength > $thelength) echo "...";  
    
    	?>  
    
    	</a> 
    
          </h2>
    PHP:
    Which does actually truncate the title prior to display on the screen, the problem I have now is that I need this code to actually replace the original title & permalink with the truncated version.
    Any help would be fantastic thanks
     
    Taffy1957, Jan 24, 2011 IP
  12. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #12
    just replace all

    the_title()

    to

    get_the_title();

    in my version :D

    BTW read my PM ;)
     
    G3n3s!s, Jan 24, 2011 IP