After much trial and error, I was able to figure this out. When you’re browsing a category, the main template file being called is taxonomy-product_cat.php which pulls archive-product.php. Basically, you add conditions to taxonomy-product_cat.php to redirect to a different template based on which level of category you’re on. The below example goes three levels deep, but you can customize based on your needs.
// We need to get the top-level category so we know which template to load.
$get_cat = $wp_query->query['product_cat'];
// Split
$all_the_cats = explode('/', $get_cat);
// How many categories are there?
$cat_count = count($all_the_cats);
//
// All the cats say meow!
//
// Define the parent
$parent_cat = $all_the_cats[0];
// In-house classes
if ( $parent_cat == 'classes' ) woocommerce_get_template( 'archive-product.php' );
// Online courses (videos) -- "top-level" categories
elseif ( $parent_cat == 'online-courses' && $cat_count == 2 ) woocommerce_get_template( 'archive-online-courses-top.php' );
// Online courses (videos) -- sub-categories
elseif ( $parent_cat == 'online-courses' && $cat_count > 2 ) woocommerce_get_template( 'archive-online-courses-sub.php' );
Thanks so much for trying this out for me! Although, I appear to be having a couple of issues.
I removed this from my taxonomy-product_cat.php:
woocommerce_get_template( 'archive-product.php' );
And added this (per your code):
// We need to get the top-level category so we know which template to load.
$get_cat = $wp_query->query['product_cat'];
// Split
$all_the_cats = explode('/', $get_cat);
// How many categories are there?
$cat_count = count($all_the_cats);
//
// All the cats say meow!
//
// Define the parent
$parent_cat = $all_the_cats[0];
// Main Wedding Invitations
if ( $parent_cat == 'wedding-collections' ) woocommerce_get_template( 'archive-product.php' );
// Collection Layout
//elseif ( $parent_cat == 'wedding-collections' && $cat_count == 2 ) woocommerce_get_template( 'subcat-archive-product.php' );
You can see all the categories I am working with here:
Link to picture of categories
I am wanting the very top product category “Wedding Collections” to have archive-product.php, then the subcategories of “Wedding Collections” (the only one right now being “Modern Collection”) to have subcat-archive-product.php.
I believe I missed something. I tried your code every way I could and couldn’t get it to work pass the “Collection Layout” part above. It still takes the layout of archive-product.php even though I click on “Modern Collection.” When clicking on “Modern Collection” right now it should only read: TEST SUBCAT ARCHIVE PRODUCT with only the header and footer showing. What did I blow up?
Test site: http://test.januarycreative.com/invitation/ (Click on Wedding Invitations to see Wedding Collections category, click “Modern Collection” to see Modern Collection products)
(Also, I have checked, subcat-archive-product.php and archive-product.php are in the same location with taxonomy-product_cat.php, so there is no worry about it not finding subcat-archive-product because it can find archive-product.php)
Thanks again for your help!!!
I need to get to the solution step by step for different template of products for specific category
I need your help 🙂
thank you very much
Really Fantastic conversation !
Now I just wanna know something to display different layout for product (single product page) under the these categories,….
hmm k let me define,
if category A display AA template for subcategories also the single product layout in AA , similarly
if category B display BB template for subcategories also the single product layout in BB
where I get all layout correctly except the “Single-Product.php”
Hi, imevilduckie can you please suggest me the technique ? thx in advance 😉
Okie got it fixed
http://wordpress.stackexchange.com/questions/3507/custom-field-values-in-permalink/3517#3517
<?php
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'audio', $categories ) ) {
echo 'In audio';
woocommerce_get_template_part( 'content', 'single-product' );
} elseif ( in_array( 'elektro', $categories ) ) {
echo 'In elektro';
woocommerce_get_template_part( 'content', 'single-product' );
} else {
echo 'some blabla';
} ?>
k.devantoni – I’m not able to get the code you worked to share content between the categories and the single product pages. How were you able to get it to work? Did you just use the code you posted, or did you combine it with imevilduckie’s code?
Basically, I have a footer that I only want to appear on a parent category, ALL of its subcategories, and the single products within those categories. All of them will share this same footer. I can use imevilduckie’s code to make it appear in my footer.php on all of the main category and subcategories pages, but it won’t show on the single product page.
Thanks for any assistance!