- This topic has 30 replies, 6 voices, and was last updated 4 years, 7 months ago by
Leo.
-
AuthorPosts
-
April 25, 2020 at 1:57 am #1254638
Emma
Hi, I noticed on your ‘niche’ template, there is a category menu above the products. I would like to do a similar thing on a website I’m working on but I can’t work out how to do this and show the menu only on the product pages. Could you advise how to do it?
April 25, 2020 at 6:24 am #1254894David
StaffCustomer SupportHi there,
its custom coded using a Hook Element. These are the steps:
1. Create a new Hook Element.
1.1 Add this PHP Code:<?php $cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true, ); $product_categories = get_terms( 'product_cat', $cat_args ); if ( ! empty( $product_categories ) ) { echo '<ul class="woo-cat-nav">'; foreach ( $product_categories as $key => $category ) { printf( '<li> <a href="%1$s"> %2$s </a> </li>', get_term_link( $category ), $category->name ); } echo '</ul>'; } ?>
1.2 Select the
woocommerce_archive_description
hook from the list
1.3 Check execute PHP.
1.4 Set your Display Rules to includeProduct Archives
2. Add this CSS:
/* Woo category nav */ .woo-cat-nav { list-style-type: none; margin-left: 0; display: flex; flex-wrap: wrap; margin-bottom: 80px; } .woo-cat-nav li { padding: 5px 0; margin: 0 10px; border-bottom: 1px solid #ccc; font-size: 0.95em; text-transform: uppercase; } @media (max-width: 768px) { .woo-cat-nav { margin-bottom: 40px; } .woo-cat-nav { justify-content: center; } }
April 25, 2020 at 1:17 pm #1255510Emma
Thank you, will doing this also show the subcategories in the menu or will it only show the primary categories? (I have 3 main categories each with multiple sub categories).
April 26, 2020 at 9:08 am #1256577Leo
StaffCustomer SupportI believe it should show sub categories as well.
Can you give it a shot first?
April 27, 2020 at 1:00 am #1257258Emma
Thanks for your reply. I have managed to do this by designing the navigation using Elementor, saving it as a template and entering the shortcode using the hook. I don’t know why I didn’t think of that before. Many thanks for your help.
April 27, 2020 at 9:56 am #1258251Leo
StaffCustomer SupportNo problem 🙂
June 29, 2020 at 7:34 am #1345553Lee
Hey!
How can I adjust this element to show only primary categories only?
Thank you 🙂
June 29, 2020 at 8:18 am #1345768David
StaffCustomer SupportHi there,
include
'parent' => 0
in your$cat_args
:$cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true, 'parent' => 0, );
June 30, 2020 at 1:04 am #1346583Lee
Amazing, that worked perfectly!
Thank you 🙂
June 30, 2020 at 3:40 am #1346726Rafał
David, thank you for the Hook!
Could you develop PHP code to show only these categories where an attribute is applied, please?
For example, attribute: Manufacturer → Company_Name.
I’d like to show its archive (loop of products by Company_Name) with menu of categories to which Company_Name’s products have been added.
Will you get a power to figure it out?June 30, 2020 at 4:55 am #1346797David
StaffCustomer SupportYou’re welcome Lee.
Hi Rafal,the above function is simply looping through a list of categories. What you require would be very complicated and requires custom development i am afraid
June 30, 2020 at 7:02 am #1346950Rafał
I might suppose to 😉
Maybe with some plugin the category menu would be filtered easier? eg. Perfect Brands for WooCommerce?
OR
WooCommerce gives some possibility – maybe with a shortcode – that render filtered categories with current attribute name?June 30, 2020 at 3:31 pm #1347538Tom
Lead DeveloperLead DeveloperYou could try asking over on something like https://wordpress.stackexchange.com/. If you provide the working code above and explain how you want to adjust it, someone may have a quick solution for you.
Otherwise, something like codeable.io might be worth checking out 🙂
August 7, 2020 at 7:06 am #1391390Simon
Is it possible to only show the child category options and not the parent ones unless at root?
August 7, 2020 at 9:22 am #1391743David
StaffCustomer SupportHi there,
for your ‘root’ you could hook in the function above with the
'parent' => 0
arg
Then a separate hook for your product archives with this snippet:<?php $current_term = get_queried_object(); $parent = $current_term->term_id; $product_categories = get_term_children( $parent, 'product_cat' ); if ( ! empty( $product_categories ) ) { echo '<ul class="woo-cat-nav">'; foreach ( $product_categories as $category ) { $term = get_term( $category, 'product_cat' ); printf( '<li><a href="%1$s">%2$s</a></li>', get_term_link( $term ), $term->name ); } echo '</ul>'; } ?>
-
AuthorPosts
- The topic ‘Add a category menu to product pages’ is closed to new replies.