Search code examples
wordpresstaxonomycustom-post-type

How to Link Sub Taxonomy to products in wordpress


I have created custom post types called Internal products

I Have a page page-internal-products.php which list all the custom taxonomy for the Custom post type Internal product

On clicking on the taxonomies takes me to a page which lists the sub taxonomies for the particular parent taxonomy for which i have created the page called taxonomy-internalproducts_categories.php

On clicking on the sub taxonomy. I need to go to a page which lists all the products for this sub taxonomy. How can I achieve this?


Solution

  • You can access the currently queried object with the get_queried_object() function and then check to see if the category has a parent or not. If it has - display posts in it, if it doesn't display all categories belonging to this category.

    Here is an example code to do that:

    $category = get_queried_object();
    if ( $category->parent ) {
        // This is a sub-category
        get_template_part( 'internal-products', 'list' );
    } else {
        // This is a main category
        get_template_part( 'internal-products', 'categories-list' );
    }
    

    What this code will do is that it will include a theme file called internal-products-list.php or internal-products.php if the current category is a sub-category. Otherwise it will include either internal-products-categories-list.php or internal-products.php.

    You can of course just write all of your code within the if/else blocks - that's up to you.