Grow your CSS skills. Land your dream job.

WP: get_previous_post and get_next_post returning the same post!

  • # February 20, 2013 at 4:57 pm

    Hi guys,
    got a quite frustrating issue!

    I have post navigation to go to the next/prev post on the single of a custom post type. It works perfectly for all posts EXCEPT the very first and the very last item.
    On these pages there should only be a next OR a prev post, but in my case get_previous_post and get_next_post are returning the same post.

    Here’s my code:

    $prevPost = get_previous_post();
    $nextPost = get_next_post();

    if ($prevPost) {
    echo previous_post_link(‘%link‘);
    }
    if ($nextPost){
    echo next_post_link(‘%link‘);
    }

    Any ideas?

    # February 20, 2013 at 5:00 pm

    I have this same problem on the archive pages of my own site. Looking forward to what some smarter people have to say on the subject!

    # February 20, 2013 at 5:56 pm

    Straight from http://codex.wordpress.org/Function_Reference/get_next_post.

    < ?php
    $next_post = get_next_post();
    if (!empty( $next_post )): ?>
    ID ); ?>”>< ?php echo $next_post->post_title; ?>
    < ?php endif; ?>

    Or, I am I missing something?

    # February 20, 2013 at 6:13 pm

    Thanks, I did see that in the Codex but unfortunately get_next_post() is returning a post even when there is no ‘next’ post (and likewise for get_previous_post()), rather than null as per the docs.

    I came across the Ambrosite Next/Previous Post Link Plus http://wordpress.org/extend/plugins/ambrosite-nextprevious-post-link-plus/, which has solved my problem :)

    Cheers

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".