The MU forums have moved to WordPress.org

How to truncate a template tag? (3 posts)

  1. WilliamTully
    Member
    Posted 15 years ago #

    I'm currently dealing with a template issue where the previous/next post links are located at the top of the screen, however, the authors insist on insanely long post titles (ie. +100 characters)... Yeah, I know..

    I would like to have part of the previous/next post titles as part of the navigation (rather than just 'previous post' and 'next post'), however, the length is the issue.

    Is there any way to truncate these two tags down to say 30 characters with an ending "..." ?:

    <?php previous_post_link('&laquo; %link') ?>
    <?php next_post_link('%link &raquo;') ?>

    I'm not a developer and I haven't been able to find anything that makes sense.

  2. DeannaS
    Member
    Posted 15 years ago #

    I wrote some functions to do that in one of our themes. Here they are:

    http://pastebin.com/m57157cf4

    You'd put these in your functions.php in your theme. (I'm assuming you're doing theme developoment, yes?)

    Then instead of passing in < or >, you'd pass in truncate(title of post, numberofcharacters, trailingindicator).

    (Note that I didn't actually test it and have not ever worked with previous/next links. But, it LOOKS like you're passing in the text to link, yes?

  3. WilliamTully
    Member
    Posted 15 years ago #

    Wow. That code does appear to be what I'm looking for however, I get lost on how exactly I would rewrite

    <?php previous_post_link('&laquo; %link') ?>

    to give me the truncated output of a linked title.. For the above tag, it outputs as:

    << Linked blog post title

About this Topic

  • Started 15 years ago by WilliamTully
  • Latest reply from WilliamTully