我所拥有的是一个页面,它显示了一个类别的帖子,底部有一个上一个和下一个帖子链接。我遇到的问题是,当它到达最后一个时,我想循环回到起点。如果您在第一个帖子上,它会一直运行到最后,如果您在最后一个帖子上,它不会回到开头,如果您在最后一个帖子上,它只会显示最后一个帖子。
到目前为止,这是我的代码:
<?php
$prev_post = get_adjacent_post( true, '', true );
$next_post = get_adjacent_post( true, '', false );
$prev_post_id = $prev_post->ID;
$next_post_id = $next_post->ID;
?>
<?php if ($prev_post_id == '') {
$query = new WP_Query( array ( 'orderby' => 'ASC', 'posts_per_page' => '1', 'cat=4' ));
while($query->have_posts()):
$query->the_post();
$prev_post_id = $query->post->ID;
endwhile;
} ?>
<?php if ($next_post_id == '') {
$query2 = new WP_Query( array ( 'orderby' => 'DESC', 'posts_per_page' => '1', 'cat=4' ));
while($query2->have_posts()):
$query2->the_post();
$next_post_id = $query2->post->ID;
endwhile;
} ?>
<a href="<?php echo get_permalink($prev_post_id); ?>" class="prev-footer-item">
<div class="prev-inner">
<h3 class="gamma"><?php echo get_the_title($prev_post_id ); ?></h3>
<h4 class="delta"><?php the_field('subtitle', $prev_post_id ); ?></h4>
<div class="footer-overlay"></div>
</div>
</a>
<a href="<?php echo get_permalink($next_post_id); ?>" class="next-footer-item">
<div class="next-inner">
<h3 class="gamma"><?php echo get_the_title($next_post_id); ?></h3>
<h4 class="delta"><?php the_field('subtitle', $next_post_id); ?></h4>
<div class="footer-overlay"></div>
</div>
</a>
我敢肯定,我很明显错过了一些东西,对吧?
更新:工作上一篇文章:
[request] => SELECT SQL_CALC_FOUND_ROWS as1_posts.ID FROM as1_posts WHERE 1=1 AND as1_posts.post_type = 'post' AND (as1_posts.post_status = 'publish' OR as1_posts.post_status = 'private') ORDER BY as1_posts.post_date DESC LIMIT 0, 1
不工作 下一篇文章:
[request] => SELECT SQL_CALC_FOUND_ROWS as1_posts.ID FROM as1_posts WHERE 1=1 AND as1_posts.post_type = 'post' AND (as1_posts.post_status = 'publish' OR as1_posts.post_status = 'private') ORDER BY as1_posts.post_date DESC LIMIT 0, 1