• I use a three column layout for my WordPress and I display the 10 more recent post titles in the far right column. When the post title is too long, it wraps down to the next line; I don’t like this. Is there a way to truncate it at the end of the last word that fits, i.e. if there are six words and it line breaks after the fourth word naturally, to only display four words followed by an ellipse?

    So

    Cody Wagonner Wins Goldendale Rockcrawl Event

    becomes

    Cody Wagonner Wins Goldendale…

    I know i can dig in and do this with strlen() stuff in php but is there an easier way?

Viewing 5 replies - 1 through 5 (of 5 total)
  • what are you using to display the posts, a plugin, if so which one? If not, what wordpress function?

    Thread Starter thebindlestick

    (@thebindlestick)

    Sorry for my delayed response, it’s been too nice outside here in Seattle to stay online! Here is what I am using:

    <?php
    
    foreach ($recentposts as $post) {
    
    if ($post->post_title == '')
    
    $post->post_title = sprintf(__('Post #%s'), $post->ID);
    
    echo "<li><a href='".get_permalink($post->ID)."'>";
    
    the_title();
    
    echo '</a></li>';
    
    }
    
    ?>

    I’d like to limit the number of words that are displayed in the title, in the sidebar only, but not on the post’s page itself – limit it to what will fit on one line… not just truncate it to a certain character count; that’s easy.

    Like I said in my first post, if I have six words in the title but only four fit on a line before it is wrapped to the next line, I’d like it to stop after the fourth word and ad an ellipse (…) so if it is set to 31 characters maximum, it would change:

    WordPress Issues World Wide Press Release

    to

    WordPress Issues World Wide…

    but not just like 31 characters like this:

    WordPress Issues World Wide Pre

    So it finds 31 characters, then goes back to the end of the last word, and stops there and adds an ellipse, any ideas?

    i’ve got the same problem. anybody an idea?

    I would love some help with this as well.

    ok i’ve got it working on my site

    <?php $my_query = new WP_Query('showposts=7');
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <?php
    if (strlen(the_title('','',FALSE)) > 40) {
      $title_short = substr(the_title('','',FALSE), 0, 40);
      preg_match('/^(.*)\s/s', $title_short, $matches);
      if ($matches[1]) $title_short = $matches[1];
      $title_short = $title_short.' ...';
      }
    else
      {
      $title_short = the_title('','',FALSE);
      }
    ?>
    
    <a title="<?php echo the_title() ?>" href="<?php the_permalink() ?>"><?php echo $title_short ?></a><br>
    
    <?php endwhile; ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Recent Posts – Title Length’ is closed to new replies.