$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$terms_array = array(
'taxonomy' => 'services', // you can change it according to your taxonomy
'parent' => 0 // If parent => 0 is passed, only top-level terms will be returned
);
$services_terms = get_terms($terms_array);
foreach($services_terms as $service): ?>
<h4><?php echo $service->name; ?></h4>
<?php
$post_args = array(
'posts_per_page' => -1,
'post_type' => 'service', // you can change it according to your custom post type
'tax_query' => array(
array(
'taxonomy' => 'services', // you can change it according to your taxonomy
'field' => 'term_id', // this can be 'term_id', 'slug' & 'name'
'terms' => $service->term_id,
)
)
);
$myposts = get_posts($post_args); ?>
<ul>
<?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; // Term Post foreach ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endforeach; // End Term foreach; ?>