1

So I created some custom pagination for my single.php template to show next/previous posts at the bottom. However, the way it is working currently is linear(ie on the most recent post there is only a "next post" link and on the oldest post there is only a "previous" post link).

What I would like to do is make the loop continuous, so that on the first post, the "next post" link will be to the second newest post and the "previous post" link will be the oldest post.

Here's my pagination code:

<?php 
    $nextPost = get_next_post();
    if($nextPost) {
        $nextPostID = $nextPost->ID;
?>
    <a class="prev-post" href="<?php echo get_permalink( $nextPostID ); ?>">
        <?php pagination_next($nextPostID); ?>
    </a>
<?php } ?>

<?php 
    $prevPost = get_previous_post();
    if($prevPost) {
        $prevPostID = $prevPost->ID;
?>
    <a class="next-post" href="<?php echo get_permalink( $prevPostID ); ?>">
        <?php pagination_prev($prevPostID); ?>
    </a>
<?php } ?>
2

You could add an else to both ifs and get the first post/latest post:

<?php 
     $nextPost = get_next_post();
    if($nextPost) {
        $nextPostID = $nextPost->ID;
?>
    <a class="prev-post" href="<?php echo get_permalink( $nextPostID ); ?>">
        <?php echo $nextPost->post_title; ?>
    </a>
<?php } else {
        $first_post = get_posts( array(
            'posts_per_page'   => 1,
            'order' => 'ASC'
        ) );
        ?>
        <a class="prev-post" href="<?php echo get_permalink( $first_post[0]->ID ); ?>">
            <?php echo get_the_title( $first_post[0]->ID ); ?>
        </a>

<?php } ?>

<?php
    $prevPost = get_previous_post();
    if($prevPost) {
        $prevPostID = $prevPost->ID;
?>
    <a class="next-post" href="<?php echo get_permalink( $prevPostID ); ?>">
        <?php echo $prevPost->post_title; ?>
    </a>
<?php } else {
    $latest_post = get_posts( array(
        'posts_per_page'   => 1,
        'order' => 'DESC'
    ) );
    ?>
    <a class="next-post" href="<?php echo get_permalink( $latest_post[0]->ID ); ?>">
        <?php echo get_the_title( $latest_post[0]->ID ); ?>
    </a>
<?php } ?>

I'm assuming you want this only for posts, so on get_posts I'm leaving the default post type as post.

  • Thank you, this seems to be exactly what I'm looking for. The only issue I'm having now is that regardless of if I put DESC or ASC in the order argument, it always returns the latest post(and vice versa). Any ideas what would cause that? – user13286 Jun 20 '17 at 17:13
  • Do you mean on both $first_post and $latest_post, you always get the same post? Are all your posts with post_status publish? – dbeja Jun 20 '17 at 17:19
  • Yep, I figured it out though, I forgot to add the [0] now it's working. Thank you so much! – user13286 Jun 20 '17 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.