首页 > 解决方案 > 如何显示自定义 postype 类别中的所有帖子?

问题描述

我正在尝试显示来自单个自定义帖子类型类别的帖子。例如:我有自定义帖子类型:“课程”和此帖子类型的三个类别:课程一、课程二、课程三。我需要单独显示此类别中的所有帖子。

这是我的代码:

<?php
  $args = array (
  'post_type' => 'cource',
  'cat' => 5, // id one of category
);

 $cat_one = new WP_Query( $args );
?>

<?php while ( $cat_one->have_posts() ) :  $cat_one->the_post(); ?>      
 <div class="widget_item">
    <figure style="background-image: url(<?=$url;?>)"></figure>
    <div class="widget_meta">
        <ul>
            <li class="widget_item_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <li><time><?php the_field('evente_date'); ?></time> 
            <li class="place"><?php the_field('evente_place'); ?> <i class="fa fa-map-marker"></i></li>
        </ul>
    </div>
</div>
<?php endwhile;?>

什么也没有发生。如果我删除 'cat' => 5 所有帖子都从自定义帖子类型显示。请帮我解决这个问题。

标签: phpwordpresswordpress-theming

解决方案


在查询中使用分类参数:

$args = array (
  'post_type' => 'cource',
  'tax_query' => array(
        array(
            'taxonomy' => 'cource',
            'field'    => 'slug',
            'terms'    => 'yout-taxonomy-term-slug-here',
        ),
    ),
);

文档:https ://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters


推荐阅读