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 a Wordpress custom post type set up in my functions.php called "Research"

I have a custom taxonomy called "Classifications"

Under that taxonomy I have the terms/categories:

Old Research (parent) 
--- sub cat 1 
--- sub cat 2

I have created the file "taxonomy-Classifications-oldresearch.php" which successfully changes the parent layout for me. However, it does not hold the layout in the sub categories. The sub-category pages revert to another file taxonomy-Classifications.php

I can alter it by also creating taxonomy-Classifications-subcat1-oldresearch.php but I will have a lot of sub categories that I want to use ONE template for - the parent cat template. This way I don't have to create a new template file for every new category.

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Hook into template_include filter

add_filter('template_include', 'research_term_template');

function research_term_template( $template ) {
  if ( is_tax('classifications') ) {
    $parent = get_term_by('slug', 'oldresearch', 'classifications');
    // to improve performance you can hardcoding 'oldresearch' term id
    // $parent = 12;
    if ( term_is_ancestor_of( $parent, get_queried_object(), 'classifications' ) )
       return get_template_directory() . '/taxonomy-Classifications-oldresearch.php';
  }
  return $template;
}
share|improve this answer
    
Simply great solution. –  cybnet Sep 5 '13 at 19:36
    
thanks, @cybnet. 'template_include' is great, often underestimated, filter. –  G. M. Sep 5 '13 at 19:40
    
Would this go in my functions file or in one of the template files? –  user2612111 Sep 5 '13 at 20:06
    
functions.php @user2612111 –  G. M. Sep 5 '13 at 20:08
    
okay. I have inserted the code, but Dreamweaver is giving me an error in code at the return get_template_directory() line. –  user2612111 Sep 5 '13 at 20:09
show 6 more comments

Change template file name taxonomy-Classifications-oldresearch.php to taxonomy-Classifications.php so that it will work for both parent and child terms of Classifications custom taxonomy.

share|improve this answer
    
Hi, that would work, but I need the oldresearch category to look difference than taxonomy-Classifications.php and have a different sidebar. –  user2612111 Sep 5 '13 at 17:50
    
Then for oldresearch category use taxonomy-Classifications-oldresearch.php template and for others categories use taxonomy-Classifications.php template. –  Vinod Dalvi Sep 5 '13 at 17:55
add comment

In taxonomy-Classifications.php you can add some logic to check the term and then include a loop template specific to that term.

// seudo-code logic example
if Classifications,  
    use loop-classifactions.php  

else if Classifications -> Old Research,  
    use loop-classifications-oldresearch.php

else if Classifications -> Old Research -> Child Term,  
    use loop-classifications-oldresearch-child.php

I believe this is the solution you are after. Get template part based on custom taxonomy term

share|improve this answer
add comment

Reading the taxonomy templates hierarchy documentation seems like it is working as expected. The file taxonomy-Classifications-oldresearch.php is specific for oldresearch term and that file won't be used by any other term. The file taxonomy-Classifications.php will be used by all terms of Classifications taxonomy except by the terms with a specific template like taxonomy-Classifications-oldresearch.php.

The template taxonomy hierarchy is as follow:

  1. A term-specific template is the first template used by Wordpress (in this case taxonomy-Classifications-oldresearch.php).
  2. If no term-specific template is found, then wordpress will use taxonomy-spceific template (in this case taxonomy-Classifications.php).
  3. If no taxonomy-specific template is found, then wordpress will use a general template for all taxonomies (taxonomy.php).
  4. If taxonomy.php is not found, wordpress will use the archive.php template.
  5. Finally, if archive.php is not found Wordpress will use index.php.

To alter this behaviour you can check the parent of the current term and use get_template_part() function or include/requiere to load the template you want. There is no other solution, I think. For example, in taxonomy-Classifications.php:

$term = get_term_by( 'slug', get_query_var('term'), 'Classifications' );
if($term->parent == 'your_oldresearch_term_ID'){
   inclue(get_template_directory().'/taxonomy-Classifications-oldresearch.php');
} else {
  .....
  .....
}
share|improve this answer
    
Why wont the subcategories under Old Research default to taxonomy-Classifications-oldresearch.php instead of taxonomy-Classifications.php? –  user2612111 Sep 5 '13 at 17:52
    
beacuse oldresearch is a term, not the taxonomy, and a term-specific template is the first template used (in this case taxonomy-Classifications-oldresearch.php). See my edited answer, I've added more explanation. –  cybnet Sep 5 '13 at 18:02
    
Thanks, that does help shed some light on the terms I was using. I guess I don't know why the child terms of oldresearch don't use it's template. –  user2612111 Sep 5 '13 at 18:39
    
I would love to mark your question as correct as it was helpful, but I really dont have a solution to my problem still. I don't need to have a bunch of templates for my sub-terms. I only want one. –  user2612111 Sep 5 '13 at 19:02
    
I think you should read again my answer. I've added more. –  cybnet Sep 5 '13 at 19:29
show 3 more comments

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.