2

I really need your help.

in my single.php I had to get the next post in the same category

(which i already have by:

$in_same_cat = true;

$excluded_categories = '';

$previous = false;

$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);)

now I need the next next post and also in the opposite direction previous previous post

thanx

3 Answers 3

2

See answer here for some code that fetches several adjacent posts:

Getting the Next and Previous Posts Titles in the Sidebar?

2

Hi
Try to use the get_adjacent_post function according to this link
http://wordpress.org/support/topic/how-to-get-next-post-id
you can create a small loop that will get the ID of the next post and then use the function again on the ID that you've received to get the next next ID.

$prevPost = get_previous_post();
$i = 0;  
$num_prev_posts = 4;  
while ($i < $num_prev_posts) //needs to check if $prevPost exists. while ($i < $num_prev_posts && !empty($prevPost)) doesn't work. 
{  
    get_permalink($post->ID); //and other such functions that work on the global $post should now work.  
    $i++;  
    $prevPost = get_previous_post(); // and then there should be a check if $prevPost is empty or not  
    print_r($prevPost);  
}

this will bring the previous 4 posts. http://wordpress.org/support/topic/get-next-5-posts-from-the-current-post

2

surprisingly,I found the answer myself...

I am using the same function as I used for next/previous post

(get_adjacent_post() ) but sending the the next/previous post which I already found as a parameter

$in_same_cat = true;

$excluded_categories = '';

$previous = true;

$previous_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);

$previous_previous_post = 
get_adjacent_post($in_same_cat,$excluded_categories,$previous,$previous_post);

$previous = false;

$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);

$next_next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous,$next_post); 

but...we haven't finished yet.. we need to add this code to the function declaration in wp-includes/link-template.php

function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true,$mypost = null) {
    global  $wpdb;

//if specific post wasnt sent to function it takes the global one and checks if its empty before using it.

    if ( empty( $mypost ) )
    {
        global $post;
        if(empty( $post ))
            return null;
        $mypost=$post;
    }
//...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.