Wordpress:下一个/上一个帖子链接不起作用

时间:2016-01-26 18:08:32

标签: php wordpress

请告诉我为什么下一步以前的帖子链接无法使用以下代码?我试图一次只显示一个帖子。我试图检查不同的帖子,但无法找到类似于我的东西。请指导......

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

2 个答案:

答案 0 :(得分:6)

我认为我遇到了与您相同的问题,只需添加此链接即可使链接转到previous_post_linknext_post_link

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

祝你好运

答案 1 :(得分:0)

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

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