- This topic is empty.
-
AuthorPosts
-
November 21, 2012 at 5:59 am #40909
evu
MemberHi there!
I’m not too great at WordPress but somehow I manage to trudge along, however I can’t seem to figure this one out and surprisingly I can’t find a plugin/fix on Google to help. I’ve managed to get so far stringing parts of things I’ve found together but I’m just stuck at the last hurdle!
What I have is a page which displayed a post from a category and at the bottom there’s a previous and next post link. The problem I have is that when it gets to the last one, I want to loop back to the start. It works going to the end if you’re on the first one, just not back to beginning if you’re on the last, it’s just displaying the last post if you’re on the last post.
Here’s my code so far:
$prev_post = get_adjacent_post( true, ”, true );
$next_post = get_adjacent_post( true, ”, false );
$prev_post_id = $prev_post->ID;
$next_post_id = $next_post->ID;
?>$query = new WP_Query( array ( ‘orderby’ => ‘ASC’, ‘posts_per_page’ => ‘1’, ‘cat=4’ ));
while($query->have_posts()):
$query->the_post();
$prev_post_id = $query->post->ID;
endwhile;
} ?>
$query2 = new WP_Query( array ( ‘orderby’ => ‘DESC’, ‘posts_per_page’ => ‘1’, ‘cat=4’ ));
while($query2->have_posts()):
$query2->the_post();
$next_post_id = $query2->post->ID;
endwhile;
} ?>I’m sure it’s something quite obvious I’m missing, right? Bloody wordpress…
Looking forward to any help!
Cheers,
John.November 21, 2012 at 6:25 am #115017Watson90
MemberHi @evu
I have a function that calls this in my index.php file which is simply;
I have wrapped this code in paragraph tags and is inserted right after the endwhile; statement at the end of your loop.
November 21, 2012 at 7:22 am #115021evu
MemberHi Watson, cheers for the reply.
Unfortunately that’s not enough to get what I want. I need to filter a certain category as well as getting the permalink, the title and a custom field separately.
If only it was that easy :)
Cheers,
John.November 22, 2012 at 10:00 am #115153evu
MemberWell with the help of a cool dude on SO I got it fixed, here’s the code just incase someone else needs it :)
$prev_post = get_adjacent_post( true, ”, true );
$prev_post_id = $prev_post->ID;$next_post = get_adjacent_post( true, ”, false );
$next_post_id = $next_post->ID;
?>if(($prev_post_id === ”) || ($prev_post_id === null)) :
$query = new WP_Query(array(‘order’ => ‘DESC’, ‘posts_per_page’ => ‘1’, ‘cat=4’));
if($query->have_posts()) : while($query->have_posts()) :
$query->the_post();
$prev_post_id = get_the_ID();
endwhile;
endif;wp_reset_postdata();
endif;
if(($next_post_id === ”) || ($next_post_id === null) ) :
$query = new WP_Query(array(‘order’ => ‘ASC’, ‘posts_per_page’ => ‘1’, ‘cat=4’));
if($query->have_posts()) : while($query->have_posts()) :
$query->the_post();
$next_post_id = get_the_ID();
endwhile;
endif;wp_reset_postdata();
endif;
?> -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.