在我的循环中,我有十篇帖子,它们分别属于以下类别(slug):group-a、group-a-first和group-b。循环/查询如下所示:
查看这些帖子是否有特定类别的最佳方法是什么?
我试过了<?php if(in_category(\'group-a\')) ;?>
, 它确实有效,但它检查了每个帖子,所以我得到了三个结果:是、否、否等。
我要寻找的是,如果循环中的任何帖子都有特定的类别,它会输出yes或no。
我的查询有多个循环,因此我使用的代码如下:
<?php $args = array(\'tax_query\' => array(array(\'taxonomy\' => \'post-status\',\'field\' => \'slug\',\'terms\' => array (\'post-status-published\')))); $query = new WP_Query( $args );?>
<?php if ( $query->have_posts() ) : $duplicates = []; while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( in_category( \'first-major\' ) ) : ?>
<div class="major"><div class="major-first">
major and first - <?php the_title();?><br>
<?php $duplicates[] = get_the_ID(); ?>
</div>
<?php endif; endwhile; ?>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if (( in_category( \'major\' ) ) && (!in_category( \'first-major\'))) : if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
major - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?></div>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if (( in_category( \'major\' ) ) && (!in_category( \'first-major\'))) : if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
major - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?></div>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( in_category(\'group-a\')) :?>
yes
<?php else :?>
no
<?php endif;?>
<?php if ( in_category( \'group-a-first\' ) ) : ?>
group a and first - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if (( in_category( \'group-a\' ) ) && (!in_category( \'group-a-first\'))) : if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
group a - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( in_category( \'group-b-first\' ) ) : ?>
group b and first - <?php the_title(); ?><br>
<?php $duplicates[] = get_the_ID(); ?>
<?php endif;?>
<?php endwhile; ?>
<?php $query->rewind_posts(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
<?php the_title();?><br>
<?php endwhile; wp_reset_postdata(); endif; ?>
</section>
谢谢你,