自定义 Post 类型即使在分类为空时也显示

Custom Post Type showing even when taxonomy is blank

我正在使用 Custom Post Type 通过在不同页面中使用 Custom Taxonomy 来获取其内容。

但是,即使 post 类型没有分类,也会显示内容。

P.S。我也在使用 ACV 来获取 Taxonomy

的字段

代码如下:

<?php if( get_field('review_category_name') ):
    $taxonomy_slug = get_field('review_category_name');  //Advanced Custom Field here
 endif; ?>
<?php $args = array( 'post_type' => 'reviews','tax_query' => array(
        'taxonomy' => 'reviews_category',
        'field' => 'slug',
        'terms' => $taxonomy_slug,
    ), 'posts_per_page' => 5 );
        $loop = new WP_Query( $args ); ?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <h3><?php the_title(); ?></h3>
    <?php the_content(); ?>
</div>
<?php endwhile; ?>

我在这里遗漏了什么吗?

$taxonomy_slug 是获取自定义 post 类型 slug 的变量。

如果检查:

,则将所有内容包装在 get_field('review_category_name')
<?php if( get_field('review_category_name') ): ?>
<?php
    $taxonomy_slug = get_field('review_category_name');  //Advanced Custom Field here
    $args = array( 'post_type' => 'reviews','tax_query' => array(
            'taxonomy' => 'reviews_category',
            'field' => 'slug',
            'terms' => $taxonomy_slug,
    ), 'posts_per_page' => 5 );
    $loop = new WP_Query( $args );
?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <h3><?php the_title(); ?></h3>
    <?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>

PS 您的示例缺少 endwhile