简体   繁体   中英

Wordpress: Next/Previous post link are not working

Please let me know why Next and Previous Post links are not working on the following code? I am trying to display only one post at a time. I have tried to check on different post but not able to find something similar to what I have. Please guide...

<?php 
    $args = array( 'numberposts' => 1 );
    $lastposts = get_posts( $args );
    foreach($lastposts as $post) : setup_postdata($post);
?>

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div>
<?php if ( has_post_thumbnail() ) : ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
        <?php the_post_thumbnail(); ?>
    </a>
<?php endif; ?>
</div>

<?php the_content(); ?>
<?php endforeach; ?>
<?php comments_template();  ?>

<nav id="nav-posts">
  <div class="prev"><?php next_posts_link('PREVIOUS POSTS'); ?></div>
  <div class="next"><?php previous_posts_link('NEXT POSTS'); ?></div>
</nav>

I think I had the same problem as you, simply adding this will make the link go to previous_post_link or next_post_link

<?php previous_post_link( '%link','Previous' ) ?>
<?php next_post_link( '%link','Next' ) ?>

Best of luck

According to the get_posts documentation , you should use the following method to display prev/next links:

<?php
$postlist = get_posts( 'orderby=menu_order&sort_order=asc' );
$posts = array();
foreach ( $postlist as $post ) {
   $posts[] += $post->ID;
}

$current = array_search( get_the_ID(), $posts );
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
?>

<div class="navigation">
<?php if ( !empty( $prevID ) ): ?>
<div class="alignleft">
<a href="<?php echo get_permalink( $prevID ); ?>"
  title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
</div>
<?php endif;
if ( !empty( $nextID ) ): ?>
<div class="alignright">
<a href="<?php echo get_permalink( $nextID ); ?>" 
 title="<?php echo get_the_title( $nextID ); ?>">Next</a>
</div>
<?php endif; ?>
</div><!-- .navigation -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM