This is what functions.php is for. Add the function to functions.php, with just a slight modification that will allow you to pass in which category you want to get the children of:
function the_categories_navigations_trees( $parent ) {
$cat_data = get_categories( array(
'parent' => $parent,
'hide_empty' => 0
) );
if ( $cat_data ) {
$cat_links="";
foreach ( $cat_data as $one_cat_data) {
$cat_links .= sprintf(
'<a href="https://wordpress.stackexchange.com/questions/279749/%s">%s</a><br/>',
get_category_link( $one_cat_data->term_id ),
$one_cat_data->cat_name
);
}
printf( '<div class="nav-cat">%s</div>', $cat_links );
}
}
See the $parent
argument in the function?
Now in your template you can use this to get the output for a particular parent:
<?php the_categories_navigations_trees( get_queried_object_id() ); ?>
get_queried_object_id()
will get the ID of the category you’re currently viewing. Replace it with something specific if you need to.