Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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' );
share|improve this answer
    
Fantastic Chip. Thank you. This problem had me really stumped back in June when I asked it. I ended up taking a different approach entirely. Very useful stuff in this answer that I will definitely use in the future. –  Zach L Dec 11 '12 at 22:38
1  
I trust your WP expertise far better than my own, or I would've made the edit, but do you mean get_queried_object()? –  Zach L Dec 11 '12 at 22:41
    
Sure did. Typo. :) –  Chip Bennett Dec 12 '12 at 0:13

Check WordPress Template Hierarchy. As you can see you can use pattern taxonomy-$taxonomy-$term.php to specify template for specific taxonomy and term. In your case it will be taxonomy-tax1-term1_1.php, etc.

share|improve this answer
    
Already been there, still stumped. Your solution would require me to have a seperate php file for each term. I want each taxonomy's terms to load from one template file –  Zach L Jun 1 '12 at 19:28
    
Re-reading this I realize it might be unclear... tax1 should have 1 template file for all of its terms' archive pages. That way, when the client adds a new term, I don't have to create a new file. –  Zach L Jun 1 '12 at 19:44
    
@ZachL taxonomy-$term.php would solve that for you. Just look further down the hierarchy chain. –  Tyler Carter Dec 11 '12 at 21:50
    
not really sure what you mean by that @Chacha102 –  Zach L Dec 11 '12 at 22:37
    
@ZachL You don't have to specify the exact term, just the taxonomy. taxonomy-$tax.php. Its already built into WordPress. –  Tyler Carter Dec 12 '12 at 5:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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