First, you must get the current viewing category/term id and taxonomy name. We can get this calling get_queried_object(); function like below:
$term = get_queried_object();
Then you need to call the get_terms() function to get child categories like below: ( if you don’t want to show those categories which do not belong to any post, then set ‘hide_empty’ => true )
$child_categories = get_terms( $term->taxonomy, array(
'parent' => $term->term_id,
'hide_empty' => false
) );
if ( !empty( $child_categories ) ) {
foreach( $child_categories as $c_cat ) {
echo '<li><a href="' . esc_url( get_term_link( $c_cat, $c_cat->taxonomy ) ) . '">' . $c_cat->name . '</a></li>';
}
}
We can use the above code to get child categories/terms for posts or any custom post type(CTP) posts.