Previous and Next Post link doesn’t work

EDIT

Referring back to one of your previous questions I’ve answered, you are using custom taxonomies, so this is most likely your problem then. You then have to set the taxonomy parameter accordingly. See my original answer

ORIGINAL ANSWER

There are a few things to check here

  • Where is your next_post_link. Did you leave out by default?

  • Are you using default categories or custom taxonomies. When the $in_same_term parameter is set to true, the taxonomy parameter is by default set to category. If you are using custom taxonomies, you will have to set the taxonomy parameter to you custom taxonomy

If this all checks out, try using the twenty fourteen theme’s navigation and see what happens.

function twentyfourteen_post_nav() {
    // Don't print empty markup if there's nowhere to navigate.
    $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
    $next     = get_adjacent_post( false, '', false );

    if ( ! $next && ! $previous ) {
        return;
    }

    ?>
    <nav class="navigation post-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'twentyfourteen' ); ?></h1>
        <div class="nav-links">
            <?php
            if ( is_attachment() ) :
                previous_post_link( '%link', __( '<span class="meta-nav">Published In</span>%title', 'twentyfourteen' ) );
            else :
                previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'twentyfourteen' ) );
                next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'twentyfourteen' ) );
            endif;
            ?>
        </div><!-- .nav-links -->
    </nav><!-- .navigation -->
    <?php
}

Simply call it with twentyfourteen_post_nav() in your single.php

EDIT 2

I’ve modified your code a bit. Here is the changes

  • Don’t use echo get_the_title() and echo get_the_date(). You can just simply used the returned functions the_title() and the_date

  • I’ve removed <h4><?php previous_post_link('<p id="next-link">%link</p>', 'Next Post', TRUE); ?></h4> from inside your <article> tag

  • I’ve removed everything everything inside <div class="pagination-bottom"></div> and replaced it with the twentyfourteen navigation function

As tested, this works perfectly on my side. You should now just modify the pagination function I’ve added in my ORIGINAL ANSWER to suite your needs. Here is your single.php modified as I tested and it works. Hope this works for you too

<?php get_header(); ?>
<div class="wrapper">
    <section class="container-12">
    <?php if (have_posts()) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <div class="single-post-wrap col-8 push-2">
            <div class="single-post-feat-img">
                <figure>
                    <?php the_post_thumbnail('news-thumb-single-post') ?>
                </figure>
            </div>
            <article>
                <h4><?php the_title(); ?></h4>
                <h6><?php the_date(); ?></h6>
                <hr>
                <p><?php the_content(); ?></p>

                <hr>
            </article>
            <div class="pagination-bottom">
              <?php twentyfourteen_post_nav(); ?>
            </div>
        </div>

    <?php endwhile; ?>
<?php endif; ?>
    </section>
    <div class="clear"></div>
    <div class="push"></div>
</div>
<?php get_footer(); ?>

EDIT3

It seems that your code has nothing to do with your problem, it seems that WordPress doesn’t get the next or previous post, that is why your links doesn’t show up. Steps to debug your problem. After each step, check if your problem still exists

  • Set debug to true and check for any errors. Also read Debugging WordPress

  • Clear your browser cache by pressing Ctrl and F5. Also, if you have any cache plugins installed, clear their caches as well

  • Disable all your plugins one by one. After deactivation of a specific plugin, refresh your site and check if your problem still exists

  • If that fails, switch to one of the bundled themes like twenty fourteen. Test your site again

  • If your problem still exists, your WordPress core files might be corrupted. Ifyou suspect that, back-up your complete database, and reinstall wordpress. WordPress 3.9.2 was just released, so take the upgrade

EDIT 4

From your comments,

I reinstalled wordpress core files, i disabled all plugins, i switched to Twenty-fourteen but nothing. Then i deployed another one wordpress installation. It’s not MAMP’s fault. I’m sure its the database’s fault. Because for a fresh wordpress database the previous and next function works flawlessly

it is clearly not a problem with your code. It seems like a permalink problem, or as you said, maybe a DB problem. Download and install WP_DBManager, repair and optimize your db and see if your problem is solved.

EDIT 5

I think there might just be light in the other side of the tunnel. The user in this post had the same exact problem, and neither one of these answers to your question worked, which is rather perplexing, taking into account that these solutions have been tried and tested.

It seems that from the answer to his own question, the user has found the culprit. He is using a bulk creator to create posts, and that is causing his pagination to fail. Check out his answer here

EDIT 6 RESOLVED

As per comment from the OP, the problem was caused by duplicated posts.

Leave a Comment

tech