Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Previous and Next Post Links in WordPress

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #40909
    evu
    Member

    Hi there!

    I’m not too great at WordPress but somehow I manage to trudge along, however I can’t seem to figure this one out and surprisingly I can’t find a plugin/fix on Google to help. I’ve managed to get so far stringing parts of things I’ve found together but I’m just stuck at the last hurdle!

    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:

    $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;
    ?>

    $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;
    } ?>
    $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;
    } ?>

    ” class=”prev-footer-item”>


    ” class=”next-footer-item”>

    I’m sure it’s something quite obvious I’m missing, right? Bloody wordpress…

    Looking forward to any help!

    Cheers,
    John.

    #115017
    Watson90
    Member

    Hi @evu

    I have a function that calls this in my index.php file which is simply;

    I have wrapped this code in paragraph tags and is inserted right after the endwhile; statement at the end of your loop.

    #115021
    evu
    Member

    Hi Watson, cheers for the reply.

    Unfortunately that’s not enough to get what I want. I need to filter a certain category as well as getting the permalink, the title and a custom field separately.

    If only it was that easy :)

    Cheers,
    John.

    #115153
    evu
    Member

    Well with the help of a cool dude on SO I got it fixed, here’s the code just incase someone else needs it :)

    $prev_post = get_adjacent_post( true, ”, true );
    $prev_post_id = $prev_post->ID;

    $next_post = get_adjacent_post( true, ”, false );
    $next_post_id = $next_post->ID;
    ?>

    if(($prev_post_id === ”) || ($prev_post_id === null)) :

    $query = new WP_Query(array(‘order’ => ‘DESC’, ‘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 === ”) || ($next_post_id === null) ) :

    $query = new WP_Query(array(‘order’ => ‘ASC’, ‘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;
    ?>

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.