Forums

How do I correctly use <?php previous_post_link (); ?> in a href...? (4 posts)

  1. recyclemysoul
    Member
    Posted 5 years ago #

    Hi there.

    I am trying to use the <?php previous_post_link (); ?> call inside an <a href> tag to provide the link for a previous post on a single page. I'm trying to do this with no text, to make img navigation buttons.

    Does that make sense? =)

    Gratzi.

  2. moshu
    Member
    Posted 5 years ago #

    That tag doesn't work on Pages.
    If you meant a single post > take a look at this plugin: http://guff.szub.net/2005/01/18/next-previous-post-img/

  3. recyclemysoul
    Member
    Posted 5 years ago #

    Is there any way not to use a plugin? Can you just wrap the php tag into an href so it pulls the link for the previous and next post while an img src is the only thing displayed?

    And yes, I meant a single post (single.php from the common Kubrick theme)

  4. Kafkaesqui
    Moderator
    Posted 5 years ago #

    You can output just the url to next and previous posts with a few internal WordPress functions, and a bit of PHP. First, collecting the basic info (ID and post title) for the next and previous posts:

    <?php
    $next_post = get_next_post();
    $prev_post = get_previous_post();
    ?>

    This resides in your template within The Loop, but before the links. Then for your image links, here's an example for next post:

    <a href="<?php echo get_permalink($next_post->ID); "><img srv="/images/next.png" title="<?php echo str_replace('"', '\'', $next_post->post_title); ?>" /></a>

    The above makes use of the PHP function str_replace() to convert " to ' (and avoid any validation issues in the title attribute).

    Previous post is no different:

    <a href="<?php echo get_permalink($prev_post->ID); "><img srv="/images/previous.png" title="<?php echo str_replace('"', '\'', $prev_post->post_title); ?>" /></a>

    Note the next and previous post url stuff is duplicated in a plugin of mine:

    http://wordpress.org/support/topic/51091#post-280466

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags