Next/Last URLs in a specific WordPress parent category

I am setting up a new WordPress site, with issue-specific articles. I’m attempting to set up some next/last buttons within the posts that cycle through posts in a specific category.

In case it’s not clear, here’s what I’ve got for this issue:

Read More
Spring 2016 (slug 2016-spring) - 2 unique posts (6 total)
    Feature (slug feature-2016-spring) - 2 unique posts
    Images (slug images-2016-spring) - 1 unique post
    President's Message (slug president-2016-spring) - 1 unique post

And then this fall, I’ll have pretty much an identical setup with slugs of 2016-fall, etc.

I want my post’s next and last buttons to only cycle through posts that contain that parent category. Every Feature, for example, is assigned both the Feature and the Spring 2016 category, so they’re all listed within the parent category. But when I get to the President’s Message, I want the “Next” button to loop me back to the first post in the category.

This is the current div I’m using for my navigation; is it easy to plug this in to there?

<div class="post_nav">
    <a href="#"><div class="last">Last</div></a>
    <a href="<?php bloginfo('url'); ?>"><div class="home">&nbsp;</div></a>
    <a href="#"><div class="next">Next</div></a>
</div>

Related posts

1 comment

  1. You can use the WordPress functions previous_post_link() and next_post_link(). By default, they’ll interact with the parent categories.

    I’m assuming you have the divs for styling purposes, so go with something like this:

    <div class="post_nav">
        <?php previous_post_link('%link', '<div class="previous">Last</div>', TRUE); ?>
        <a href="<?php bloginfo('url'); ?>"><div class="home">&nbsp;</div></a>
        <?php next_post_link('%link', '<div class="next">Next</div>', TRUE); ?>
    </div>
    

Comments are closed.