WordPress.org

Forums

How to get next post ID ? (9 posts)

  1. justjob
    Member
    Posted 5 years ago #

    I know about the function next_post_link, but i need just an next post id
    Please, help me =)

  2. MichaelH
    Member
    Posted 5 years ago #

    Believe this would work in the loop of your single.php

    <?php
    $adjacent_post = get_adjacent_post(false,'',false) ;
    echo $adjacent_post->ID;
    ?>
  3. justjob
    Member
    Posted 5 years ago #

    Great, thanx!

    and this code to previous post id

    $adjacent_post = get_adjacent_post(true,'',true) ;

  4. MichaelH
    Member
    Posted 5 years ago #

    $adjacent_post = get_adjacent_post(true,'',true) ;
    That would be for next post in category

    More illustration:

    $in_same_cat = false;
    $excluded_categories = '';
    $previous = true;
    $previous_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous)
    $in_same_cat = false;
    $excluded_categories = '';
    $previous = false;
    $next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous)
  5. Coby604
    Member
    Posted 5 years ago #

    This is exactly what I'm looking for as well. However, I tried the exact code within my wordpress loop, and it didn't return anything for me?

    I'm running version 2.9.2

    I also can't find any documentation about the get_adjacent_post tag. Is this tag still supported in the current version of wordpress?

  6. Coby604
    Member
    Posted 5 years ago #

    I realized that this only returns a result on the single permalink page. So I'm wondering if there is a way for this to work on the main page as well.

  7. Coby604
    Member
    Posted 5 years ago #

    I figured out that adding this little bit of code:

    $wp_query->is_single = true;

    basically this will tell WP to treat the current page as a single page, even if it’s your home page or main page with multiple posts on the page.

  8. jcskyrocket
    Member
    Posted 4 years ago #

    Sweet, get_adjacent_post() is what I need as well. Thanks!

  9. tedcarter
    Member
    Posted 4 years ago #

    how does "$excluded_categories" work?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags