Excluding top level from taxonomy and post loop
-
I’ve got a custom post type / taxonomy. I’ve created a list that shows all the categories and the posts in them.
I have set up a top level category (taxonomy) that has two sub categories. I want my list to show only the second level categories and the posts in them. Currently it outputs both the sub categories and their posts correctly but it then also shows the top level category and all the posts within that too even if they are not directly in it.
Sub Category 1
•Post 1
•Post 2Sub Category 2
•Post 3
•Post 4Top Level Category
•Post 1
•Post 2
•Post 3
•Post 4How do I exclude the top level category and it’s list completly? E.g.
Sub Category 1
•Post 1
•Post 2Sub Category 2
•Post 3
•Post 4Current code
<?php $custom_terms = get_terms('service_categories'); foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array('post_type' => 'services', 'tax_query' => array( array( 'taxonomy' => 'service_categories', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<h2>'.$custom_term->name.'</h2>'; while($loop->have_posts()) : $loop->the_post(); $title = get_the_title(); $url = get_permalink(); $serviceicon = get_field( 'select_icon' ); ?> <div class="left-list2-box"> <a href="<?php echo $url; ?>"> <div class="left-list2-icon"> <div class="list-services-icons <?php echo $serviceicon ?>"></div> </div> <div class="left-list2-title"> <?php echo $title; ?> </div> </a> </div> <?php endwhile; } } ?>
The topic ‘Excluding top level from taxonomy and post loop’ is closed to new replies.