0

I tried to get the next and previous URL, and create a navigation. But it seems to only retrieve the permalink from the two first posts (but not the others recents posts).

I have 5 posts (ID = 25, 86, 80, 75, 45). When I am on the first post (ID = 25) and I click on previous post, I am going to post ID = 45, and after when I click on previous it stay on the same page.

<?php 
  if (have_posts()) : while (have_posts()) : the_post();

  $prev = get_permalink(get_adjacent_post(false,'',false));
  $next = get_permalink(get_adjacent_post(false,'',true)); ?>

  <li class="post-link">
    <a class="prev" href="<?php echo $prev; ?>">
      <span class="icon-prev"></span>
    </a>
  </li>

  <li class="post-link">
    <a class="next" href="<?php echo $next; ?>">
      <span class="icon-next"></span>
    </a>
  </li>

<?php endwhile; endif; get_footer(); ?>

I also tried to put the code after the while loop.

Update I solved the problem. When I published my posts, they had all the same date, so it displayed only one post from this date. I changed with a different day date, and it works! My bad.

  • is that in an index template or in the single.php template? have you reviewed codex.wordpress.org/Pagination – Michael Feb 8 '14 at 20:50
  • Yes, it's on the index page. I already take a look at this page, but I didn't find anything to fix the problem I think. – jbr Feb 8 '14 at 21:50
1

You could use different code. As an example, this modified code from the Twenty Fourteen parent theme which i tested on other themes works well.

Add this to a new file named template-tags.php

<?php

if ( ! function_exists( 'wpsites_post_nav' ) ) :

function wpsites_post_nav() {

$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">
    <div class="nav-links">
        <?php
            previous_post_link( '%link', __( '<span class="meta-nav">Previous Post</span>%title', 'wpsites' ) );
            next_post_link( '%link', __( '<span class="meta-nav">Next Post</span>%title', 'wpsites' ) );
        ?>
    </div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php
}
endif;

Creates a template tag which you can use in a custom function or directly in your single.php file:

Child themes functions.php file

require_once( get_stylesheet_directory() . '/template-tags.php' );

Alternatively - Parent themes functions.php file

require get_template_directory() . '/template-tags.php' ;

Single.php template tag

<?php wpsites_post_nav(); ?>
| improve this answer | |
  • I wrote an update on my first post. Thanks anyway ;) – jbr Feb 9 '14 at 11:27
0

You should use & for the navigation. http://codex.wordpress.org/Template_Tags/next_post_link http://codex.wordpress.org/Template_Tags/previous_post_link

Thanks

| improve this answer | |
  • Already test, inside the while loop, after, nothing works. Plus I don't want to get the full <a> tag, just the link for the previous and next post. – jbr Feb 9 '14 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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