0

I have a custom taxonomy called product-categories. Currently all categories use the master template (taxonomy-product-categories.php).

The category tree is roughly as follows.

Products 
- Cheese
-- Types of Cheese
--- Cheddar
--- Brie
--- Cheshire
-- Types of Milk
--- Cows Cheese
--- Goats Cheese
- Cakes
- Category

I want to set it so that all categories, including sub_categories in the cheese section of the tree use a cheese template (taxonomy-product-categories-cheese.php). With other templates for the other areas.

I've found a two year old post which appears to be the solution but does not appear to do anything.

Make Custom Taxonomy Category Use Parent Template

I've added the following to my functions.php

add_filter('template_include', 'cheese_term_template');

function cheese_term_template( $template ) {
  if ( is_tax('classifications') ) {
    // $parent = get_term_by('slug', 'cheese', 'classifications');
    $parent = 13;
    if ( term_is_ancestor_of( $parent, get_queried_object(), 'classifications' ) )
       return get_template_directory() . '/taxonomy-product-categories-cheese.php';
  }
  return $template;
}

I though that it was looking at the top level category instead but when I did some tests it didn't work with that either. I wonder if it was because the taxonomy name has a hyphen in it.

I'd appreciate any help... Thanks

2
  • Is your taxonomy called classifications or is it called product-categories ? The latter sounds like the rewrite slug.
    – Howdy_McGee
    Commented Jun 1, 2015 at 21:52
  • Ahhh thank you... I didn't realise that "classifications" should have been the taxonomy name... I either looked at it for too long or it just looked like it was supposed to be there. I changed "classifications" to "product-categories" and it all started to work..
    – Legin76
    Commented Jun 1, 2015 at 23:46

1 Answer 1

2

WordPress has a Template Hierarchy - specifically for taxonomies and terms. So what template name you want should follow along the lines of:

taxonomy-{$taxonomy}-{$term-slug}.php

In your case:

taxonomy-classifications-cheese.php

This will grab onto the cheese category and display this specific template. I'm not entirely sure if this will also catch child categories ( I would test the above first before trying the below ) but if it does not you can:

function cheese_template_redirect( $template ) {

    if ( is_tax( 'classifications' ) ) {
        $term   = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
        $cheese = 13;   // This should be "Cheese" Category
        if ( term_is_ancestor_of( $cheese, $term->term_id, 'classifications' ) ) {
            $new_template = locate_template( array( 'taxonomy-classifications-cheese.php' ) );
            if ( '' != $new_template ) {
                return $new_template ;
            }
        }
    }

    return $template;
}
add_filter( 'template_include', 'cheese_template_redirect', 99 );

It looks like your issue is that you're using the actual taxonomy rewrite slug, which we don't want to use. I haven't tested the above code but if it doesn't work or if you run into issues, let me know.

1
  • I've not tested the above as the previous comment made me realise my error. Thank you.
    – Legin76
    Commented Jun 1, 2015 at 23:51

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.