WordPress.org

Ready to get started?Download WordPress

Forums

Pagination loop posts so always showing both previous/next? (4 posts)

  1. jezThomp
    Member
    Posted 1 year ago #

    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?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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; ?>
  3. jezThomp
    Member
    Posted 1 year ago #

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

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic