• Resolved dhermanq

    (@dhermanq)


    I’m currently developing a WordPress theme for use as an artist’s portfolio. Posts are individual paintings, which are grouped into Categories.

    At the top of each post, navigation links allow the user to go to the Next/Previous painting (post) within the category.

    Currently, when the user navigates to the first (most recent) or last (oldest) Post in the Category, the “Previous” or “Next” navigation links disappear, respectively. What I’d like to have happen is for the link text to remain, but to be grayed out and inactive. To do this, I’m trying to figure out how to write a conditional statement in the ‘Single’ template that would check to see if the current Post is the most recent or oldest Post in the Category. But so far I’ve been unsuccessful.

    I’ve searched around and seen numerous examples of using variables as counters, however this is only useful when there are multiple Posts displayed on one page. I need to find out if the current single post is the most recent or oldest Post in the Category.

    Is there something simple I’m overlooking? I appreciate any insight you might have.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Use get_adjacent_post()

    Set the $in_same_cat variable to true. Do this also with the functions: previous_post_link() and next_post_link() in your single.php template file.

    Here is an example based on the twenty eleven theme single.php: https://pastebin.com/q3TqZ8jS

    Thread Starter dhermanq

    (@dhermanq)

    That was it!

    get_adjacent_post() worked like a charm.

    Here is the code I ended up using:

    <div class="navigation">
    	<?php if (get_adjacent_post( true, '', false) == '') : ?>
    		<div id="prev-nav-gray">&lang;&lang; PREV  </div>
    	<?php else : ?>
    		<div id="prev-nav"><?php next_post_link('%link', '&lang;&lang; PREV  ', TRUE); ?></div>
    	<?php endif ?>
    
    	<?php if (get_adjacent_post( true, '', true) == '') : ?>
    		<div id="next-nav-gray">  NEXT &rang;&rang;</div>
    	<?php else : ?>
    		<div id="next-nav"><?php previous_post_link('%link', '  NEXT &rang;&rang;', TRUE); ?></div>
    	<?php endif ?>
    </div>

    Thank you keesiemeijer!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditional: is current single post newest/oldest post in category?’ is closed to new replies.