Support » Fixing WordPress » Custom archive template for taxonomy subcategories

  • Resolved startsmartnow

    (@startsmartnow)


    I’m developing a website that has a custom post type ‘products’ with a custom taxonomy ‘product categories’ and under ‘product categories’ there is a category called ‘car decals’ and it’s the parent category for a sub-category called ‘animal decals’. I developed a custom taxonomy archive template for ‘car decals’ and it’s called taxonomy-prcat-car-decals.php.

    The problem is that the template is NOT being applied to the subcategories under ‘car decals’. I have figured out that I can just re-name the template taxonomy-prcat-animal-decals.php to get the template to apply to the animal decals archive page.

    HOWEVER, there are about 25 subcategories under ‘car decals’ – and they all need the same template.

    HOW DO I…name a template so that it will apply to the ‘car decals’ archive and all its child categories?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi there,

    I’d suggest making a template called taxonomy-prcat.php’ (I assume that prcat is the name of the product categories taxonomy) and then handle the logic you describe inside of that template. Here’s about what you’d do in that template (note: this is incomplete code since I don’t have all of the details, but it should give you the idea)

    `
    //the term
    $current_term = $wp_query->queried_object;
    $animal_decals_term_id = /** code to get the animal decals term ID **/
    $animal_decals_children_ids = get_term_children( $animal_decals_term_id, ‘product_categories’ ); /// note adjust the second arg so it’s the correct name of the taxonomy

    if ( $current_term->ID === $animal_decals_term_id || in_array( $current_term->ID, $animal_decals_children_ids) ) {
    get_template_part( /** YOUR SPECIAL TEMPLATE **/);
    }else{
    get_template_part( /** THE NORMAL ARCHIVE TEMPLATE **/);
    }

    `

    There are many ways to do this, but this would be one of them — good luck!

    Best,

    Matt

    Thread Starter startsmartnow

    (@startsmartnow)

    Thanks for the reply. I thought there might be some way to handle this within template. As I’ve started expanding the products and categories, I’ve realized that I needed to setup things up differently. I’m removing product categories. I’m making car decals a custom taxonomy and putting animal decals (along with the 25 other categories) under car decals. Now, I have a template called taxonomy-car-decals.php and this template applies to all the categories within this taxonomy. Problem solved.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom archive template for taxonomy subcategories’ is closed to new replies.