• Hi there,
    In a line such as this:

    <?php previous_post_link(‘%link’, ‘%title’, FALSE, ’14’) ?>

    How would I set ‘%title’ to be, say, 20 characters maximum (so as to not have link titles that are long and wrap around)?

    The logic would be:

    – If ‘%title’ characters > 20
    – Then limite ‘%title’ to 20 characters and add ‘…’ text afterwards

    But… I don’t know how to write this in PHP.

    Thank you in advance if you have the time,
    Berklie

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m not sure if this will help or not, but I did this up for a custom theme before…

    In the theme’s functions.php file, I have this:

    <?php 
    
    function the_title2($before = '', $after = '', $echo = true, $length = false) {
             $title = get_the_title();
    
          if ( $length && is_numeric($length) ) {
    
                 $title = substr( $title, 0, $length );
    
              }
    
            if ( strlen($title)> 0 ) {
    
                 $title = apply_filters('the_title2', $before . $title . $after, $before, $after);
    
                 if ( $echo )
    
                    echo $title;
    
                 else
    
                    return $title;
    
              }
    
          }
    
    ?>

    Then, in the theme’s index.php file, I have this:

    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title2('', '...', true, '20') ?></a></h1>

    That will give you 20 characters total, that’s including the last three “…” in the “20”. If you want “20” actual title characters you could “up” that “20” to “24” and you’ll have a full 20 characters for title and then “…” will be after it anyway…

    As for this: <?php previous_post_link('%link', '%title', FALSE, '14') ?> I’m not really sure, I haven’t tested it on localhost yet. I hope this is something you could work from though…? =) Maybe a PHP guru in here can help you better though. Good luck.

    NOTE: If you already have post titles that aren’t long to begin with, it won’t display anymore then what you already have length wise. Obviously. =P

    Thread Starter berklieblog

    (@berklieblog)

    Hey!
    I’m just seeing this reply now… I’ll definitely give it a shot at the end of the month (when I’m back from traveling).

    Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Limit ‘%title’ to 20 characters’ is closed to new replies.