How To Limit Number of Characters In Title...?

Discussion in 'PHP' started by upshiftmedia, Apr 21, 2007.

  1. #1
    If you go to theblogconnection.com, you will see in the recent entries column,

    that the longer post titles runs into a second line. How can I limit the number of

    characters displayed? So that the titles can be more like "25 Ways To Improve

    Your...." instead of "25 Ways To Improve Your Sites Popularity." What is the

    code to do this?
     
    upshiftmedia, Apr 21, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    $maxlen = 40;
    
    if (strlen($title) > $maxlen)
    {
        $title = substr($title, 0, $maxlen) . '...';
    }
    
    
    PHP:
     
    nico_swd, Apr 21, 2007 IP
    fsmedia likes this.
  3. upshiftmedia

    upshiftmedia Peon

    Messages:
    469
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, Im still learning the php thing. Where would I put this in my code:

    <h3>Recent Entries</h3>
    <ul class="dates">
    	<?php
    
    		// I love Wordpress so
    		query_posts('showposts=7');
    	?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<li><a href="<?php the_permalink() ?>"><span class="date"><?php the_time( $hemingway->date_format() ) ?></span> <?php the_title() ?> </a></li>
    	<?php endwhile; endif; ?>
    </ul>
    PHP:
     
    upshiftmedia, Apr 21, 2007 IP
  4. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #4
    upshiftmedia, does the last bit of code you posted even work..?
    I can see a lot of parsing errors in it.. from first sight.
     
    Meth_, Apr 21, 2007 IP
  5. upshiftmedia

    upshiftmedia Peon

    Messages:
    469
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ya it is placed in its own block. So it does work. So where would I place the code?
     
    upshiftmedia, Apr 21, 2007 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Use PHP built in wordwrap function.

    
    <h3>Recent Entries</h3>
    <ul class="dates">
        <?php
    
            // I love Wordpress so
            query_posts('showposts=7');
        ?>
    
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><span class="date"><?php the_time( $hemingway->date_format() ) ?></span> <?php wordwrap(the_title(), 40, "<br/>");); ?> </a></li>
        <?php endwhile; endif; ?>
    </ul>
    
    Code (markup):
    Edit: The way you wrote your post made me think you wanted wordwrap (since you actually wrap your posts :S).

    Peace,
     
    Barti1987, Apr 21, 2007 IP
  7. upshiftmedia

    upshiftmedia Peon

    Messages:
    469
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hmm...that didn't work
     
    upshiftmedia, Apr 21, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Okay, since I don't know anything about WP, I'll try it the cheesy way:

    
    
    <h3>Recent Entries</h3>
    <ul class="dates">
        <?php
    
            // I love Wordpress so
            query_posts('showposts=7');
        ?>
    
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><span class="date"><?php the_time( $hemingway->date_format() ) ?></span>
    
    <?php 
    
    ob_start();
    the_title();
    $title = ob_get_contents();
    ob_end_clean();
    
    $maxlen = 40;
    
    if (strlen($title) > $maxlen)
    {
        $title = substr($title, 0, $maxlen) . '...';
    }
    
    echo $title;
    
     ?> </a></li>
        <?php endwhile; endif; ?>
    </ul>
    
    
    PHP:
    Not sure if that's gonna work, but give it a try.
     
    nico_swd, Apr 21, 2007 IP
  9. upshiftmedia

    upshiftmedia Peon

    Messages:
    469
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ok so I have decided to use the Fuzzy Recent Posts by semiologic to fix the problem. Now the only issue that I am having is styling the plugin to look like the other two blocks. I know I have to edit the plugin in code, but i'm not sure where to add the <ul class> and the <li> to create the lines.

    Thanks Everyone for the help.
     
    upshiftmedia, Apr 21, 2007 IP
  10. upshiftmedia

    upshiftmedia Peon

    Messages:
    469
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I love you! It worked perfectly. I got rid of the plug-in and used this. Thank you for the help. :)

    Mike

     
    upshiftmedia, Apr 24, 2007 IP