Custom post types and category archive

I think this should be a simple thing to do but I’m struggling to make it work.

What I have is the following:

Read More

Custom page type called ‘products’.

Products contain multiple categories and sub-categories.

The categories and sub-categories contain posts (products).

I want a custom template page to list the root categories which I’ve achieved by adding a template file called ‘archive-products.php’ – this now lists all root categories within the products post type.

Now, when I select a category I want to display a different template to list all sub-categories and products within that category but I can’t seem to get it to load the correct template file. I thought it should load the ‘archive-products.php’ file and I could detect the category and show a different template but I can’t seem to figure out how to do it.

I’m hoping this is something simple I’ve missed but I’ve been staring at the problem for so long I can’t seem to find a solution!

Thanks in advance.

Related posts

1 comment

  1. If I understand correctly you have something like so:

    • Category A (Using Template 1)
      • Sub-Category 1 (Using Template 2)
      • Sub-Category 2 (Using Template 2)
    • Category B (Using Template 1)

    You might need to add this to your archive-products.php as a conditional to test if a category has children, and if so show a different layout or design:

    <?php 
        $this_category = get_category($cat);
        if (get_category_children($this_category->cat_ID) != "") {
            // Category has NO children
        }
        else{
            // Category HAS children
        }
    ?>
    

    Found this answer here: different-template-for-subcategory-any-conditions by triplebull

    Hopefully it helps!

Comments are closed.