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 2
Sub Category 2
- Post 3
- Post 4
Top Level Category
- Post 1
- Post 2
- Post 3
- Post 4
How do I exclude the top level category and it's list completly? E.g.
Sub Category 1
- Post 1
- Post 2
Sub Category 2
- Post 3
- Post 4
The code I used is below.
Ref: Loop through custom taxonomies and display posts
<?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;
}
}
?>