简体   繁体   English

Wordpress中的连续上一个和下一个帖子链接

[英]Continuous Previous and Next Post Links in Wordpress

What I have is a page which displayed a post from a category and at the bottom there's a previous and next post link. 我所拥有的页面显示了一个类别的帖子,底部有一个上一个和下一个帖子链接。 The problem I have is that when it gets to the last one, I want to loop back to the start. 我的问题是,当到达最后一个时,我想循环回到起点。 It works going to the end if you're on the first one, just not back to beginning if you're on the last, it's just displaying the last post if you're on the last post. 如果您在第一个帖子上,它会一直运行到末尾;如果在最后一个帖子上,它不会回到开始,如果您在最后一个帖子上,它只会显示最后一个帖子。

Here's my code so far: 到目前为止,这是我的代码:

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

I'm sure it's something quite obvious I'm missing, right? 我敢肯定我很想念这件事,对吗?

Update: Working Prev Posts: 更新:上一个工作职位:

[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

Not Working Next Posts: 下一篇文章不起作用:

[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

I can't see anything that is jumping out at me, but I've added an if statement to both of your loops (to prevent errors), added wp_reset_postdata() after each loop, and removed the need for $query and $query2 - you can just use $query for both if you don't wish to use the query results for anything other than this. 我看不到有什么让我wp_reset_postdata() ,但是我已经在两个循环中添加了一个if语句(以防止错误),在每个循环后添加了wp_reset_postdata() ,并消除了对$query$query2 -如果您不希望将查询结果用于除此以外的任何内容,则只能对这两者使用$query

Another thought - are you certain get_adjacent_post() returns '' for the Post ID if it doesn't exist? 再想一想-如果帖子ID不存在,您确定get_adjacent_post()返回''吗? Could it be null , or '0' for example. 可以为null ,例如为'0' May be worth doing var_dump($next_post_id) and checking. 可能值得做var_dump($next_post_id)并进行检查。

Edit 编辑

I forgot to mention, get_adjacent_post() assumes that the post_type of the adjacent post is the same the one you are currently viewing. 我忘了提一下, get_adjacent_post()假设相邻帖子的post_type与您当前正在查看的帖子相同。 I can't see this being an issue though, as you've already said it works the other way around, but it's something to bear in mind for the future. 不过,我不认为这是一个问题,因为您已经说过了,但是这是未来的事情。

<?php
if($prev_post_id === '') :

    $query = new WP_Query(array('orderby' => 'ASC', 'posts_per_page' => '1', 'cat=4'));

    if($query->have_posts()) : while($query->have_posts()) :
        $query->the_post();
        $prev_post_id = get_the_ID();
        endwhile;
    endif;

    wp_reset_postdata();

endif;

if($next_post_id === '') :

    $query = new WP_Query(array('orderby' => 'DESC', 'posts_per_page' => '1', 'cat=4'));

    if($query->have_posts()) : while($query->have_posts()) :
            $query->the_post();
            $next_post_id = get_the_ID();
        endwhile;
    endif;

    wp_reset_postdata();

endif;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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