Wordpress 如何显示自定义分类法父项和子项(包括帖子)

Wordpress 如何显示自定义分类法父项和子项(包括帖子),wordpress,categories,custom-post-type,taxonomy,children,Wordpress,Categories,Custom Post Type,Taxonomy,Children,我目前正在从事一个Wordpress项目,该项目必须显示自定义帖子类型和分类中的所有类别、子类别和帖子。 我应该变成这样: 第一类 子类别 职位1 职位2 子类别 职位3 第2类 子类别 邮政4 此时,代码返回所有类别和子类别的列表 在h3标签之间的分类中。此处仅应显示父类别 <?php $terms = get_terms('resource_category', array('hierarchical' => fal

我目前正在从事一个Wordpress项目,该项目必须显示自定义帖子类型和分类中的所有类别、子类别和帖子。 我应该变成这样:

  • 第一类
    • 子类别
      • 职位1
      • 职位2
    • 子类别
      • 职位3
  • 第2类
    • 子类别
      • 邮政4
此时,代码返回所有类别和子类别的列表 在h3标签之间的分类中。此处仅应显示父类别

    <?php
    $terms = get_terms('resource_category', array('hierarchical' => false));
    foreach ($terms as $term) {

     $cat_slug = $term->slug;
     $cat_id = $term->term_id;
     $subcats = get_categories('child_of='.$cat_id.'&taxonomy=resource_category');
     if ( have_posts() ) :

     /* CATEGORY */ ?>
     <div class="resources">
     <?php echo '<h3>'.$term->name.'</h3>';

       /* SUBCATEGORY */
       foreach ($subcats as $subcat) {
       if ( have_posts() ) :
       echo '<h4>' . $subcat->name .'</h4>';
       query_posts('post_type=resources&resource_category='.$subcat->cat_name.'&hide_empty=1'); ?>
       <?php while ( have_posts() ) : the_post(); 

       /* SUBCATEGORY POSTS */?>
       <div class="resource-item">
       <ul>
         <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
       </ul>
       </div>
       <?php endwhile; endif; wp_reset_query();} ?>
       </div>

     <?php endif; wp_reset_query(); } ?>


如果有人能帮我的话,非常感谢

要解决第一个问题,请将参数更改为
get_terms()
-您告诉它不要获取hierarchical,但实际上您需要做的是将其设置为:
array('parent'=>0)
,这将导致它只获取顶级术语。