1

before we start, thank's for your time.

Structure:
cpt = corsi
tax = argomenti
tax_term = parent
tax_term_child = child
tax_term_child_granchild = grandchild

Current code (on taxonomy-argomenti.php):

$queried_object = get_queried_object();
$cpt = get_post_type( get_the_ID() );
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;

    $current_term = get_term_by('id', get_query_var('term'), $taxonomy);
    if((int)$current_term->parent)
        get_template_part('argomenti', 'child');
    else
        get_template_part('argomenti', 'parent');

I would choose what template use for all level of taxonomy term.

Example:

localhost\cpt                           archive-cpt.php
localhost\cpt\parent                    argomenti-parent.php
localhost\cpt\parent\child              argomenti-child.php
localhost\cpt\parent\child1\grandchild  argomenti-grandchild.php

Is possible with a function like the one I've posted?

Thank's a lot.

1 Answer 1

2

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 = '/' . filter_var( $subfolder, FILTER_SANITIZE_STRING ) . '/';

    /**
     * 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' );
13
  • This works!! Can be this function edited to use the get_tax_hierarchy_template_part(); like this get_tax_hierarchy_template_part( 'argomenti'); to replace the first name of template before "-" with the one I want directly from the taxonomy-slug.php ?? I hope is comprehensible.
    – Simo
    Commented Nov 19, 2015 at 8:56
  • So basically the first part of the template name should be the taxonomy name. Commented Nov 19, 2015 at 9:10
  • Yes, I think I will use it always like that ... but maybe I have to use another slug. Is possible to use get_tax_hierarchy_template_part($taxonomy); or also get_tax_hierarchy_template_part( 'The_Slug_I_Need'); ?
    – Simo
    Commented Nov 19, 2015 at 9:13
  • IS AWESOME and it works perfect!! Thanks a lot for your time. Have a nice day!!
    – Simo
    Commented Nov 19, 2015 at 9:32
  • My pleasure, glad it worked. Enjoy :-) Commented Nov 19, 2015 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.