get_previous_post() retrieves current post??
-
I’m having some trouble using the function
get_previous_post()when trying to implement post navigation on thesingle.phppage.At the moment, it is retrieving the post that is currently being displayed on the
single.phppage.I am using
get_next_post()function in conjunction with this and that function seems to work perfectly fine, just trouble withget_previous_post().Has anyone else encountered trouble of the same kind and could give me some advice?
An excerpt of the code used:
<!– Previous post –>
<?php// Get previous post
$previousPost = get_previous_post();// Check to see if it is there is a post
if (!empty($previousPost)) {
// Previous post available// Get full post data
$post = get_post($previousPost->ID);setup_postdata($post);?>
(Display post in HTML below)As I said, the data in
$postis the same of that of the data of the page’s current post. I know that there isn’t a problem with double assignment or overriding with the$postvariable as I am using it before forget_next_post()with no problems.Any help will be greatly appreciated.
Thanks
-
How and where are you checking? Your use of
$post(which is a global in the current context) andsetup_postdatacan really throw things off.What you should be doing to test this is:
$current_post = get_post(); $previous_post = get_previous_post(); assert( $current_post->ID != $previous_post->ID );Nothing in between and no misuse of the
$postglobal. Does the above work?Thanks for the response!
I’ve used
$postoutside of the main loop, and usedwp_reset_postdata();after the main loop so thought that it would be fine to use it as I have been, which is why I am confused to why it isn’t working.When I use your test, it returns true, I did:
if (assert($current_post->ID != $previous_post->ID )) { die(‘Same post’); }
and it did display the
diemessage.Any suggestions ?
No, that’s not how assert works. http://php.net/manual/en/function.assert.php
assert will return true if the condition is true. So:
if (assert($current_post->ID != $previous_post->ID )) { die('Not the same post'); }Or perhaps just:
if ($current_post->ID == $previous_post->ID) { die('Same post'); }It won’t die there. The fact that your code above died and displayed the message means that they’re actually not the same post, and that everything worked.
So please don’t use the global
$postorreset_postdata, you’re mixing everything up it seems, and polluting the namespace.Ah sorry, thank you for your detailed answer, I appreciate it.
You are correct though, it didn’t die when I did the second if statement. However, it still is showing the same post in my previous post display.
I think you are right, I will not use the global namespace and try tidy things up, then it might start to work.
Thanks
Is there anything you would recommend to get the previous post and all of its data? I would use just
get_previous_post()but I want post data such as featured image.Thanks
get_previous_postis a valid way to get the previous post. The featured image can be retrieved using https://developer.ww.wp.xz.cn/reference/functions/get_the_post_thumbnail/ and giving it the ID of the previous post.
The topic ‘get_previous_post() retrieves current post??’ is closed to new replies.