get next next post in single.php
-
16-10-2019 - |
Question
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
Solution 3
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;
}
//...
OTHER TIPS
See answer here for some code that fetches several adjacent posts:
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