I solved it, after facing these 2 issues:
1- use template_include
in filter instead of taxonomy_template
2- I reordered these lines according to my need.
// Current first
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
// Parent second
$templates[] = "category-{$parent->slug}.php";
$templates[] = "category-{$parent->term_id}.php";
$templates[] = 'category.php';
So here is my final code:
/**
* Filter the taxonomy hierarchy to inject a parent level of templates.
*
* @param string $template The current template.
* @return string Filtered taxonomy template.
*/
function new_tax_hierarchy( $template ) {
$term = get_queried_object();
// If not an object, or the object doesn't have a taxonomy, bail.
if ( ! is_object( $term ) || ! isset( $term->taxonomy ) )
return $template;
$taxonomy = $term->taxonomy;
// If the taxonomy isn't hierarchical, bail.
if ( ! is_taxonomy_hierarchical( $taxonomy ) )
return $template;
$templates = array();
$parent_id = $term->parent;
if ( 0 == $parent_id ) {
// Use default values from get_taxonomy_template().
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
$templates[] = 'taxonomy.php';
} else {
$parent = get_term( $parent_id, $taxonomy );
// Parent templates.
$templates[] = "taxonomy-$taxonomy-{$parent->slug}.php";
$templates[] = "taxonomy-$taxonomy-{$parent->term_id}.php";
$templates[] = 'taxonomy.php';
// Current templates.
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
}
return locate_template( $templates );
}
add_filter( 'template_include', 'new_tax_hierarchy' );