Grow your CSS skills. Land your dream job.

Previous and Next Post Links in WordPress

  • evu
    # November 21, 2012 at 5:59 am

    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:

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

    ” class=”prev-footer-item”>

    < ?php echo get_the_title($prev_post_id ); ?>

    < ?php the_field('subtitle', $prev_post_id ); ?>


    ” class=”next-footer-item”>

    < ?php echo get_the_title($next_post_id); ?>

    < ?php the_field('subtitle', $next_post_id); ?>

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

    Looking forward to any help!

    Cheers,
    John.

    # November 21, 2012 at 6:25 am

    Hi @evu

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

    < ?php previous_posts_link('Previous Entries'); ?>< ?php next_posts_link('Older Entries'); ?>

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

    evu
    # November 21, 2012 at 7:22 am

    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.

    evu
    # November 22, 2012 at 10:00 am

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

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

    < ?php
    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)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".