WordPress:下一篇文章/上一篇文章链接不起作用


Wordpress: Next/Previous post link are not working

请让我知道为什么下一篇文章上一篇文章链接不适用于以下代码?我尝试一次只显示一个帖子。我试图检查不同的帖子,但找不到与我拥有的类似的东西。请指导...

<?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>

我想我和你有同样的问题,简单地添加这个会使链接转到previous_post_linknext_post_link

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

祝你好运

根据get_posts文档,您应该使用以下方法来显示上一个/下一个链接:

<?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 -->