WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Pull the ID of a previous and next post in single view? [WP 2.5]

[Resolved] Pull the ID of a previous and next post in single view? [WP 2.5]

  • buffawhat
    Member

    @buffawhat

    I’m trying to pull the post ID of the previous and next post when viewing a single post. Instead of simple links, I want to display the thumbnail (stored in a custom field) of the next and previous posts. All I need is the Post ID’s.

    First off, I looked at this but I can’t get it to return any values. I added the functions for get_next_post_id and get_prev_post_id in my functions.php in the template.

    <?php
    function get_next_post_id() {
    global $wp_query;
    if ($wp_query->current_post == $wp_query->post_count) return 0;
    $next_post = $wp_query->posts[$wp_query->current_post + 1];
    return $next_post->ID;
    }
    
    function get_prev_post_id() {
    global $wp_query;
    if ($wp_query->current_post == $wp_query->post_count) return 0;
    $prev_post = $wp_query->posts[$wp_query->current_post - 1];
    return $prev_post->ID;
    }
    
    ?>

    Does anyone have an idea of where I’m failing?

Viewing 1 replies (of 1 total)
  • That code won’t work on single post pages. Use get_previous_post and get_next_post instead.

    $previous_post = get_previous_post();
    echo $previous_post->ID;

Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] Pull the ID of a previous and next post in single view? [WP 2.5]’ is closed to new replies.