2
votes

What I have is a page which displayed a post from a category and at the bottom there's a previous and next post link. The problem I have is that when it gets to the last one, I want to loop back to the start. It works going to the end if you're on the first one, just not back to beginning if you're on the last, it's just displaying the last post if you're on the last post.

Here's my code so far:

<?php 
$prev_post = get_adjacent_post( true, '', true );
$next_post = get_adjacent_post( true, '', false );
$prev_post_id = $prev_post->ID;
$next_post_id = $next_post->ID;
?>

<?php if ($prev_post_id == '') {
$query = new WP_Query( array ( 'orderby' => 'ASC', 'posts_per_page' => '1', 'cat=4' ));

  while($query->have_posts()):
       $query->the_post();
       $prev_post_id = $query->post->ID;
  endwhile;
} ?>
<?php if ($next_post_id == '') {
  $query2 = new WP_Query( array ( 'orderby' => 'DESC', 'posts_per_page' => '1', 'cat=4'    ));
  while($query2->have_posts()):
       $query2->the_post();
       $next_post_id = $query2->post->ID;
  endwhile;
} ?>

<a href="<?php echo get_permalink($prev_post_id); ?>" class="prev-footer-item">
  <div class="prev-inner">
    <h3 class="gamma"><?php echo get_the_title($prev_post_id ); ?></h3>
    <h4 class="delta"><?php the_field('subtitle', $prev_post_id ); ?></h4>
    <div class="footer-overlay"></div>
  </div>
 </a>
<a href="<?php echo get_permalink($next_post_id); ?>" class="next-footer-item">
  <div class="next-inner">
    <h3 class="gamma"><?php echo get_the_title($next_post_id); ?></h3>
    <h4 class="delta"><?php the_field('subtitle', $next_post_id); ?></h4>
    <div class="footer-overlay"></div>
  </div>
</a>

I'm sure it's something quite obvious I'm missing, right?

Update: Working Prev Posts:

[request] => SELECT SQL_CALC_FOUND_ROWS  as1_posts.ID FROM as1_posts  WHERE 1=1  AND as1_posts.post_type = 'post' AND (as1_posts.post_status = 'publish' OR as1_posts.post_status = 'private')  ORDER BY as1_posts.post_date DESC LIMIT 0, 1

Not Working Next Posts:

[request] => SELECT SQL_CALC_FOUND_ROWS  as1_posts.ID FROM as1_posts  WHERE 1=1  AND as1_posts.post_type = 'post' AND (as1_posts.post_status = 'publish' OR as1_posts.post_status = 'private')  ORDER BY as1_posts.post_date DESC LIMIT 0, 1
1
+1 for posting your code - so many people don't! - David Gard
In that code, 'orderby' => 'ASC' seems to make no sense? E.g., 'orderby' => 'title', 'order' => 'ASC'? - user3445853

1 Answers

0
votes

I can't see anything that is jumping out at me, but I've added an if statement to both of your loops (to prevent errors), added wp_reset_postdata() after each loop, and removed the need for $query and $query2 - you can just use $query for both if you don't wish to use the query results for anything other than this.

Another thought - are you certain get_adjacent_post() returns '' for the Post ID if it doesn't exist? Could it be null, or '0' for example. May be worth doing var_dump($next_post_id) and checking.

Edit

I forgot to mention, get_adjacent_post() assumes that the post_type of the adjacent post is the same the one you are currently viewing. I can't see this being an issue though, as you've already said it works the other way around, but it's something to bear in mind for the future.

<?php
if($prev_post_id === '') :

    $query = new WP_Query(array('orderby' => 'ASC', 'posts_per_page' => '1', 'cat=4'));

    if($query->have_posts()) : while($query->have_posts()) :
        $query->the_post();
        $prev_post_id = get_the_ID();
        endwhile;
    endif;

    wp_reset_postdata();

endif;

if($next_post_id === '') :

    $query = new WP_Query(array('orderby' => 'DESC', 'posts_per_page' => '1', 'cat=4'));

    if($query->have_posts()) : while($query->have_posts()) :
            $query->the_post();
            $next_post_id = get_the_ID();
        endwhile;
    endif;

    wp_reset_postdata();

endif;
?>