仅在 Wordpress 自定义 Post 类型页面上显示单个类别

Showing a single Category only on a Wordpress Custom Post Type page

我已经为我网站上的一个页面制作了带有自定义字段等的自定义 post 类型...目前它工作正常并显示了我创建的所有课程:http://seedcreativehub.co.uk/book-seedcreativehub-courses/

但是,我现在只希望页面显示一个特定的课程类别。

我正在使用的代码如下,我认为它就像在 'category_name' => 'upcoming-event' 下方添加一样简单 post 类型,但是它似乎根本没有显示任何内容...

我想不出我做错了什么,如果有人能提供帮助,将不胜感激。

        <?php
          $args = array(
            'post_type' => 'courses',
            'category_name' => 'upcoming-event'
          );
          $the_query = new WP_Query( $args );
        ?>

        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

          <div class="col-xs-3 course">

            <?php
              $thumbnail_id = get_post_thumbnail_id();
              $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
            ?>

            <p><a href="<?php the_permalink(); ?>"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php the_title(); ?>" graphic></a></p>
            <h3><?php the_field('event_date'); ?></h3>
            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>


          </div>


          <?php $portfolio_count = $the_query->current_post + 1; ?>
          <?php if ( $portfolio_count % 4 == 0): ?>

          </div><div class="row">

          <?php endif; ?>


          <?php endwhile; endif; ?>

感谢观看,

肖恩。

根据分类查询帖子时,您必须使用 tax_query 参数。

    <?php
      $args = array(
        'post_type' => 'courses',
        'tax_query' => array(
            array(
                'taxonomy' => 'course-category',  // Taxonomy name
                'field'    => 'slug',
                'terms'    => 'upcoming-event',   // Term name
            ),
        ),
      );
      $the_query = new WP_Query( $args );
    ?>