• Currently the default WordPress pagination will not show a previous button if its the first post on the single.php

    Is there anyway to have the posts just loop forever round, so the first post has a next button to the next post and a previous to the last post. For the purpose of a gallery of posts?

    Currently i have..

    <li class="previous"><?php previous_post_link('%link','<span></span> Prev Project') ?></li>
     <li class="next"><?php next_post_link('%link','Next Project <span></span>') ?></li>

    So just both buttons are always there to navigate in a loop?

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

    (@keesiemeijer)

    Try replacing your code with this:

    <?php
    $previous  = get_adjacent_post( false, '', true );
    if(!$previous) :
      $first_post = get_posts('posts_per_page=1&post_status=publish');
      if($first_post) : ?>
       <li class="previous">
       <a href="<?php echo get_permalink($first_post[0]->ID); ?>"><span></span> Prev Project</a>
       </li>
      <?php endif; ?>
    <?php else : ?>
      <li class="previous"><?php previous_post_link('%link','<span></span> Prev Project') ?></li>
    <?php endif; ?>
    <?php
    $next  = get_adjacent_post( false, '', false );
    if(!$next) :
      $last_post = get_posts('posts_per_page=1&post_status=publish&order=ASC');
      if($last_post) : ?>
       <li class="next">
       <a href="<?php echo get_permalink($last_post[0]->ID); ?>"><span></span> Next Project</a>
       </li>
      <?php endif; ?>
    <?php else : ?>
      <li class="next"><?php next_post_link('%link','Next Project <span></span>') ?></li>
    <?php endif; ?>

    Thread Starter jezthomp

    (@jezthomp)

    Thank you, works a treat even for custom posts when you add in their query 🙂

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you got it resolved 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pagination loop posts so always showing both previous/next?’ is closed to new replies.