我目前正在开发一个Wordpress项目,该项目必须显示自定义帖子类型和分类的这些子类别中的所有类别,子类别和帖子 . 我应该成为这样的东西:

  • 类别1

  • subcategory

  • 帖子1

  • 帖子2

  • subcategory

  • 帖子3

  • 类别2

  • subcategory

  • 帖子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(); } ?>

非常感谢有人可以帮助我!