https://developer.wordpress.org/files/2014/10/template-hierarchy.png
Here you can view template file name that’s best fits for you.
https://codex.wordpress.org/Function_Reference/get_term_children
Use these function to get current term ( category ) childs. Note that in taxonomy.php
template you can use $wp_query->get_queried_object_id();
to get current term_id
you’r viewing
Use new WP_Query
to echo posts inside the child terms loop to get posts from child term new WP_Query( array( '{taxonomy}' => {child_term_id} ) );
Here’s an example:
taxonomy.php ( or better category.php if it’s category taxonomy your using )[missing get_header() and get_footer()]
global $wp_query;
$cat_id = $wp_query->get_queried_object_id();
$cat_childs = get_term_children( $cat_id, 'category' );
if( $cat_childs ) {
foreach ( $cat_childs as $cat_child ) {
$child = get_term_by( 'id', $cat_child, 'category' );
$child_posts = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $child->term_id,
'include_children' => false
)
)
) );
if( $child_posts->have_posts() ) {
echo '<h1>' . $child->name . '</h1>';
while( $child_posts->have_posts() ) : $child_posts->the_post();
the_title();
echo "\r\n";
endwhile;
wp_reset_postdata();
}
}
} else {
// there's no category childs
}