Support » Fixing WordPress » If statements for Previous/Next links in single page

  • Resolved Valerie



    I am working with the Connections theme and I would like to write if statements for previous and next posts links in the sidebar.

    To keep the style consistent with the theme, I am trying to create a sentence around each link, and I don’t want the entire thing to be hyperlinked. But I also don’t want any of the one sentence or the other to show up if there is no previous or next post to go with it. I thought an if statement would be the way to go. Unfortunately I still have a very long way to go in understanding php.

    Here’s what I tried:

    <?php if ($previous_post) { ?>
    <li>You can go to the <?php previous_post('%', 'Previous Post: ', 'yes'); ?>. </li>
    <?php } ?>
    <?php if ($next_post) { ?>
    <li>You can go to the <?php next_post('%', 'Next Post: ', 'yes'); ?>.</li>
    <?php } ?>

    Nothing from within that code shows up on the page, whether or not there are previous or next posts available. Can anyone tell me what I need to do to get this to work the way I want? Is there another way to do it?

    Thank you! 🙂

    (BTW: The Using Specific Text example code on the Codex page for the “next post” tag does not work as shown.)

Viewing 5 replies - 1 through 5 (of 5 total)
  • I figured out what to do. Yeah! 🙂

    I just had to qualify my if statement with true. It was actually something I tried just before posting here, but I wrote it wrong that time. Just because I don’t have a good grasp of basic php rules, yet.

    Anyway, here’s the code that works for me:

    <?php if ($previous_post = true) { ?>
    <br />
    <li>You can go to the <?php previous_post('%', 'Previous Post: ', 'yes'); ?>. </li>
    <?php }; ?>
    <?php if ($next_post = true) { ?>
    <br />
    <li>You can go to the <?php next_post('%', 'Next Post: ', 'yes'); ?>.</li>
    <?php }; ?>

    I’m learning.

    Sigh. I thought I had it working. But looking at it now, it’s clear I do not.

    From what I have been reading, the single equal sign should have never worked for it would then attempt the assign the value of ‘true’ to the tags. So I’ve tried the double equal sign, and I’ve tried putting single quotes around “true”. But then, I begin to wonder about the need to include “true” at all. The $post tag doesn’t need it in an if statement, so why should these tags? That puts me back to square one.

    It’s got me thinking that maybe I shouldn’t be trying to use the next_post, previous_post tags inside an if statement.

    I’m doing my homework & have been trying to come up with the answer on my own. I suspect there is an easy solution, but I just haven’t figured it out.

    I sure wish I hadn’t posted what turned out to be a non-solution – I was trying to follow the forum rules. Now it’s going to make my cry for help less likely to be spotted by those who can help. It’s going to be worse since I have to post again to let others know that the above does NOT work.

    I know you guys have day jobs and do most of your helping – with NO pay! – on the weekends. And I really appreciate all the effort and time you give to this forum. Thank you!

    OK. I’ll go back to my seat, now. 🙂

    I’m just wondering if anyone if anyone is willing to give me a solution to my above request. Thank you. 🙂

    From the Description of this template tag:
    “This tag must be used in The Loop.”


    I do understand that this is the case. And the code actually brings up the previous and next post titles & links when they exist.

    Oh, nevermind. It all works correctly within the previous and next tags, if one uses the parameters correctly. 😛

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘If statements for Previous/Next links in single page’ is closed to new replies.