get_next_post_link() and get_previous_post_link() return wrong posts

admin2025-01-07  5

I'm using get_next_post_link() and get_previous_post_link() within the loop but the returned posts are not correct.

get_next_post_link() shows the previous post and get_previous_post_link() gives the current post. Below the context of these links:

$args['name'] = $postname;
$query = new WP_Query($args);

if($query->have_posts())
    {
    while ($query->have_posts())
        {
        $query->the_post();

        $id         = get_the_ID();
        $title          = get_the_title();
        $content        = get_the_content();

        $nextpost       = get_next_post_link('Next: %link');
        $previouspost   = get_previous_post_link('Prev: %link');
        }
    }

I'm using get_next_post_link() and get_previous_post_link() within the loop but the returned posts are not correct.

get_next_post_link() shows the previous post and get_previous_post_link() gives the current post. Below the context of these links:

$args['name'] = $postname;
$query = new WP_Query($args);

if($query->have_posts())
    {
    while ($query->have_posts())
        {
        $query->the_post();

        $id         = get_the_ID();
        $title          = get_the_title();
        $content        = get_the_content();

        $nextpost       = get_next_post_link('Next: %link');
        $previouspost   = get_previous_post_link('Prev: %link');
        }
    }
Share Improve this question asked Sep 18, 2015 at 21:21 bartbart 2251 gold badge2 silver badges9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

This has been solved by changing Site Address (URL) in Settings > General to the path that matches with the posts's path. I was pulling in posts from another location in my website that did not match with the original (default) path of the post.

get_next_post_link();
get_previous_post_link();

These work on single posts for navigation from one single post to another NOT pagination for archive pages unless you use get_query_var( 'paged' )

I think you can not pass proper argument. Please check this link for reference : https://codex.wordpress.org/Function_Reference/get_next_posts_link

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264799a995.html

最新回复(0)