1

In single.php I tried to add inside the loop the previous_post_link and next_post_link. But nothing is rendered when a view the source of the site. I added the previous_post_link inside an h4 tag to see it better. Normally I'll add it in pagination-bottom. Why is this happening?

Here is my code

    <?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 echo get_the_title(); ?></h4>
                <h6><?php echo get_the_date(); ?></h6>
                <hr>
                <p><?php the_content(); ?></p>
                <h4><?php previous_post_link('%link', 'Pre Post', TRUE); ?></h4>
                <hr>
            </article>
            <div class="pagination-bottom">
                <div class="pre-post">
                <a href=""><i class="fa fa-angle-left"></i>Προηγούμενο</a>
                </div>
                <div class="next-post"><a href="">Επόμενο<i class="fa fa-angle-right"></i></a>
                </div>
            </div>
        </div>   
    <?php endwhile; ?>
<?php endif; ?>

3 Answers 3

1

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.

0
0

The third parameter in your previous_post_link() function call is set to true, which will restrict any results to those posts in the same category as the current post. Try setting the third parameter to false (or remove it entirely), and see if that makes a difference.

If the third parameter is intentional, make sure you actually have posts in the same category as the current post. If you are using a custom taxonomy, you need to specify the custom taxonomy name as the fifth parameter:

previous_post_link( '%link', 'Pre Post', TRUE, '', 'my_custom_taxonomy' );
3
  • nope :/ didn't work
    – hambos22
    Commented Aug 8, 2014 at 19:53
  • Do you actually have other posts in the same category as the current post? Does it work with the third parameter set to false (or not included)? Commented Aug 8, 2014 at 20:05
  • Yeah. It's only one category and i have about 20 posts there. I tried with many variations of the code but none of them worked
    – hambos22
    Commented Aug 9, 2014 at 0:23
0

Replace all your pagination code with this and change the classes to match your CSS or you could search for the classes included in this code in the Twenty Twelves themes style.css file.

<nav class="nav-single">
<span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentytwelve' ) . '</span> %title' ); ?></span>
<span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentytwelve' ) . '</span>' ); ?></span>
</nav><!-- .nav-single -->

Basic CSS

.nav-previous {
    float: left;
    width: 50%;
}

.nav-next {
    float: right;
    text-align: right;
    width: 50%;
}

Source: Twenty Twelve

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.