Using a different template per Custom Taxonomies for single term archive pages

I have 2 custom taxonomies, each with a number of terms:

tax1
  term1_1
  term1_2
tax2
  term2_1
  term2_2
  term2_3

I am trying to have each taxonomy usea different template file for each its single-term archive pages. I.E. term1_1 and term1_2 use one template, while term2_1 - 3 use a second one. I can of course just load different php files conditionally from the main archive.php file – but I would like to know if there is a way to do this within the standard template hierarchy.

Read More

As far as I can tell, taxonomy-tax1.php doesn’t work for this (unless I’m messing up royally)

Related posts

Leave a Reply

3 comments

  1. I would intercept the template loader at template_redirect:

    function wpse53892_taxonomy_template_redirect() {
        // code goes here
    }
    add_action( 'template_redirect', 'wpse53892_taxonomy_template_redirect' );
    

    Then, you would check to see if the query is a custom taxonomy:

    if ( is_tax() ) {
        // This query is a custom taxonomy
    }
    

    Next, get the term object, and find out if it has a parent:

    // Get the queried term
    $term = get_queried_object();
    
    // Determine if term has a parent;
    // I *think* this will work; if not see below
    if ( 0 != $term->parent ) {
        // Tell WordPress to use the parent template
        $parent = get_term( $term->parent );
        // Load parent taxonomy template
        get_query_template( 'taxonomy', 'taxonomy-' . $term->taxonomy . '-' . $parent->slug . 'php' );
    }
    
    // If above doesn't work, this should:
    $term_object = get_term( $term->ID );
    if ( 0 != $term_object->parent; {}
    

    So, putting it all together:

    function wpse53892_taxonomy_template_redirect() {
    
        // Only modify custom taxonomy template redirect
        if ( is_tax() ) {
            // Get the queried term
            $term = get_queried_object();
    
            // Determine if term has a parent;
            // I *think* this will work; if not see above
            if ( 0 != $term->parent ) {
                // Tell WordPress to use the parent template
                $parent = get_term( $term->parent );
                // Load parent taxonomy template
                get_query_template( 'taxonomy', 'taxonomy-' . $term->taxonomy . '-' . $parent->slug . 'php' );
            }
        }
    }
    add_action( 'template_redirect', 'wpse53892_taxonomy_template_redirect' );
    
  2. Modify the conditionals to target you archive page. I would use template_include like this :

    add_filter( 'template_include', 'tax_term_template', 99 );
    
    function tax_term_template( $template ) {
    
        if ( "add your conditional here"  ) {
            $new_template = locate_template( array( 'tax_term.php' ) );
            if ( '' != $new_template ) {
                return $new_template;
            }
        }
    
        return $template;
    }
    

    I would NOT use template redirect as advised by Lead Developer Mark Jaquith.