[resolved] Pull the ID of a previous and next post in single view? [WP 2.5] (2 posts)

  1. buffawhat
    Posted 8 years ago #

    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.

    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?

  2. 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;

Topic Closed

This topic has been closed to new replies.

About this Topic