0

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.

4 Answers 4

3

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;
}
11
  • Simply great solution.
    – cybmeta
    Sep 5, 2013 at 19:36
  • thanks, @cybnet. 'template_include' is great, often underestimated, filter.
    – gmazzap
    Sep 5, 2013 at 19:40
  • Would this go in my functions file or in one of the template files?
    – streetfire
    Sep 5, 2013 at 20:06
  • functions.php @user2612111
    – gmazzap
    Sep 5, 2013 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.
    – streetfire
    Sep 5, 2013 at 20:09
0

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.

2
  • Hi, that would work, but I need the oldresearch category to look difference than taxonomy-Classifications.php and have a different sidebar.
    – streetfire
    Sep 5, 2013 at 17:50
  • Then for oldresearch category use taxonomy-Classifications-oldresearch.php template and for others categories use taxonomy-Classifications.php template. Sep 5, 2013 at 17:55
0

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

0
0

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 {
  .....
  .....
}
8
  • Why wont the subcategories under Old Research default to taxonomy-Classifications-oldresearch.php instead of taxonomy-Classifications.php?
    – streetfire
    Sep 5, 2013 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.
    – cybmeta
    Sep 5, 2013 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.
    – streetfire
    Sep 5, 2013 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.
    – streetfire
    Sep 5, 2013 at 19:02
  • I think you should read again my answer. I've added more.
    – cybmeta
    Sep 5, 2013 at 19:29

Your Answer

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

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