0

I have created several custom menus that appear under the main menu on the shop page. Each category (and its subcategories) should display a different custom menu like below:

custom menu: category1 shows menu1, category2 shows menu2... and so on.

I tried to achieve this by renaming the taxonomy-product_cat.php file to each category name in order to create different templates, but this doesn't seem to affect anything.

So I'm guessing the best solution is to modify archive-product.php and add a conditional if (is_product_category( 'cat1' )) that shows the menu if category "cat1" is being displayed. It seems to be a simple solution but I can't make it work.

Any feedback is very much appreciated.

3
  • What about WP core function like is_tax() as if( is_tax( 'product_cat', 'cat1' )). Details Nov 26, 2015 at 10:33
  • Thanks! That's working for me. One more question: is there a way to target all the sub-categories inside the parent category using the same function or similar?
    – Rod Doe
    Nov 26, 2015 at 11:10
  • 1
    If that works for you, please take the time to answer your own question with some example code for later readers. Then ask a new question for your previous comment. Thanks!
    – kaiser
    Nov 26, 2015 at 12:25

1 Answer 1

1

This is the solution I found that works:

As usual in these cases, override the archive product template from your woocommerce plugin folder by copying archive-product.php to yourtheme/woocommerce/archive-product.php. Edit the file, and add this code at the beginning (or wherever the menu needs to show up):

<?php 

if ( is_tax( 'product_cat', 'cat1' )) {
    wp_nav_menu( array( 'theme_location' => 'menu1' ) ); 
}

elseif ( is_tax( 'product_cat', 'cat2' )) {
    wp_nav_menu( array( 'theme_location' => 'menu2' ) ); 
}

else ( is_tax( 'product_cat', 'cat3' )) {
    wp_nav_menu( array( 'theme_location' => 'menu3' ) ); 
}

?>

menu1 will be displayed when we're on the products page that belongs to category cat1.

menu2 will be displayed when we're on the products page that belongs to category cat2.

And so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.