get_template_part for each level of taxonomy term

Creating and including a template for your custom post type archive is easy, you just need to create a archive-{$post_type}.php template, WordPress will automatically use that template whenever you visit that particular post type’s archive page. Just remember to set the has_archive parameter when registering your post type.

As for the taxonomy archive page, you are almost there, it is just a matter of checking if a term is top level or a child or grandchild term. What I can read from your question, you have already created a taxonomy-{$taxonomy}.php template which you will use for all terms from the taxonomy argomenti, regardless of hierarchy. You have used template parts which you will include according to hierarchy.

So lets look at the logic we will be using here.

  • get_queried_object() will hold the term object of the term currently being viewed. We will use this to get the term’s parent and term ID

  • All top level terms will have a parent ID of 0, which is important.

  • For any term with parent ID more than 0, we will need to determine where it fits in in the hierarchy. For this, we will use get_ancestors. This function will return an array of term ID’s. The first term ID will be the term being tested direct parent, and the last term ID will be that of the top level term, the parent. To determine where in the hierarchy a term fits in, we just need to get the size of the array, an empty array means parent, an array with one key means child and an array with two keys means grandchild

We can now put all that logic into code. To avoid a bunch of code in your taxonomy template, we will create a function that you can add in functions.php and then call a single line into your taxonomy template. (NOTE: All code is untested and might be buggy, so be sure to test this locally with debug turned on. Also, you would require PHP 5.4+)

function get_tax_hierarchy_template_part( $template="", $subfolder="" )
{
    /**
     * Make sure we are actually on a taxonomy archive page, if not, return false
     * 
     * Instead of returning false here, you can also set it to return a default template
     * Check the section commented out
     */
    if ( !is_tax() )
        return false;
        // return get_template_part( 'content' ); // Return default template

    // Get the current term object
    $current_term_object = get_queried_object();

    // Check if we have a value for $subfolder, if so, sanitize it and add slashes
    if ( $subfolder )
        $subfolder="https://wordpress.stackexchange.com/" . filter_var( $subfolder, FILTER_SANITIZE_STRING ) . "https://wordpress.stackexchange.com/";

    /**
     * Check if we have value for $template, if so, sanitize, if not, use the taxonomy name
     * Also append the $subfolder to $template
     */
    if ( $template ) {
        $template = filter_var( $template, FILTER_SANITIZE_STRING );
    } else {
        $template = $current_term_object->taxonomy;
    }
    $template = $subfolder . $template;
    // Check if current term is top level, if so, return template part for parent terms
    if ( $current_term_object->parent == 0 )
        return get_template_part( $template, 'parent');

    /**
     * If we have reached this section, it means our term is not toplevel
     * We must now determine where in the hierarchy the term is
     */
    $hierarchy = get_ancestors( $current_term_object->term_id, $current_term_object->taxonomy );

    // We must now get the size of the array
    $hierarchy_depth = count( $hierarchy );

    /**
     * We will set child when the size of the array is one. For any size more 
     * than one, we will set grandchild
     *
     * If you are going to have grand-grandchildren which should have its own
     * template, you would need to adjust this section
     */
    $part = ( $hierarchy_depth == 1 ) ? 'child' : 'grandchild';

    // Return the correct template part according to hierarchy
    return get_template_part( $template, $part );
}

You can now just call it as follow in your taxonomy template

get_tax_hierarchy_template_part();

If you have set a fall back, you can use it anywhere

EDIT

As requested, I have included a parameter in the function called $template. You can now set the template name for the template part. If this is parameter is not set, the taxonomy name will be used.

USAGE

If your template part has the following name, {$taxonomy}-parent.php, you need to call your function as follow

get_tax_hierarchy_template_part();

If your template part is called anything else, for instance content-parent.php, you need to call your function as follow

get_tax_hierarchy_template_part( 'content' );

EDIT 2

Due to the conditions whereby the taxonomy name should be used as default for a template part, I had to incorporate a second parameter called $subfolder in order to accomodate template parts in subfolders. I have updated the code accordingly.

USAGE

You need to pass only the subfolder name without slashes as second parameter. Remember to pass an empty string as first parameter if you are using {$taxonomy}-parent.php as template part

Example:

get_tax_hierarchy_template_part( '', 'subfoldername'  );

For content-parent.php type template parts, use

 get_tax_hierarchy_template_part( 'content', 'subfoldername' );