bart
bart

Reputation: 15308

get_next_post_link() and get_previous_post_link() return wrong posts

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');
        }
    }

Upvotes: 0

Views: 3752

Answers (2)

Sindhu
Sindhu

Reputation: 88

<?php 
$prev_post = get_previous_post();
if (!empty( $prev_post )): ?>

    <div class="nav-previous alignleft">
        <a href="<?php echo get_permalink( $prev_post->ID ); ?>">« Previous</a>
    </div>

<?php endif; 

$next_post = get_next_post();
if ( is_a( $next_post , 'WP_Post' ) ) { ?>

    <div class="nav-next alignright">
        <a href="<?php echo get_permalink( $next_post->ID ); ?>">Next »</a>
    </div>

<?php } ?>

Upvotes: 2

bart
bart

Reputation: 15308

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.

Upvotes: 0

Related Questions