Php 如何在wp_查询循环中获取下一篇文章的类别

Php 如何在wp_查询循环中获取下一篇文章的类别,php,wordpress,Php,Wordpress,我需要检查循环中的当前帖子是否与下一个帖子具有相同的类别 while($query->have_posts()){ $query->the_post(); if(category_of_this_post == category_of_the_next_post) //this //do something } 大致设置如下所示 while ( $query->have_posts() ) : $query->the_po

我需要检查循环中的当前帖子是否与下一个帖子具有相同的类别

while($query->have_posts()){
    $query->the_post();
        if(category_of_this_post == category_of_the_next_post) //this
        //do something
    }

大致设置如下所示

while ( $query->have_posts() ) : $query->the_post();

    $next                 = $query->posts[ $query->current_post + 1];
    $current_categories   = get_the_category();
    $next_post_categories = get_the_category($next->ID);

    if ( /* compare $current_categories  $next_post_categories */ ) {

    }
endwhile;
wp_reset_postdata(); // always reset
需要记住的几件事:

  • 您可以分配多个类别,因此请考虑如何进行比较
  • 在最后一篇文章中,你会得到一个通知。因为它不存在。建立一个检查

    • 大致设置如下

      while ( $query->have_posts() ) : $query->the_post();
      
          $next                 = $query->posts[ $query->current_post + 1];
          $current_categories   = get_the_category();
          $next_post_categories = get_the_category($next->ID);
      
          if ( /* compare $current_categories  $next_post_categories */ ) {
      
          }
      endwhile;
      wp_reset_postdata(); // always reset
      
      需要记住的几件事:

      • 您可以分配多个类别,因此请考虑如何进行比较
      • 在最后一篇文章中,你会得到一个通知。因为它不存在。建立一个检查

      这在过去对我来说很有效,可以将上一个和下一个放在同一只猫里:

       <?php previous_post_link( '%link', __( '<span class="prev"><< Previous Post</span>', 'esquire' ), TRUE ); ?>
       <?php next_post_link( '%link', __( '<span class="prev">Next Post >></span>', 'esquire' ), TRUE ); ?>
      

      这在过去对我来说很有效,可以将上一个和下一个放在同一只猫里:

       <?php previous_post_link( '%link', __( '<span class="prev"><< Previous Post</span>', 'esquire' ), TRUE ); ?>
       <?php next_post_link( '%link', __( '<span class="prev">Next Post >></span>', 'esquire' ), TRUE ); ?>
      

      不要像上面那样,按类别获取帖子,并为此创建一个数组。同一类别的所有帖子都将以单子数组的形式出现。问题是我真的需要这样做,而不是像上面那样,按类别获取帖子并为此创建一个数组。同一类别的所有帖子都将以单子数组的形式出现。问题是我真的需要这样做。我在遍历所有帖子时,它们是按类别分组的,当下一篇帖子与当前帖子来自不同的类别时,我只想显示一些内容并保持循环,我正在遍历所有帖子,它们是按类别分组的,当下一篇文章来自与当前文章不同的类别时,我只想显示一些东西并保持循环going@SaintBurrito如果这是正确答案,请按原样标记。@SaintBurrito如果这是正确答案,请按原样标记。