- This topic has 9 replies, 2 voices, and was last updated 3 years, 6 months ago by David.
-
AuthorPosts
-
September 9, 2020 at 4:23 pm #1437346Fredrik
Hello,
I’m adding this element hook (see below) to woocommerce_archive_description (borrowed from site library Niche) to toggle between product categories. Works as expected π
But could it be possible to somehow only use the toggle for a specific parent category?
Right now I have two parent categories as menu items in the navigation. So ideally, I could navigate to parent category #1 and toggle only the categories in that parent category. Navigate to parent category #2 and only toggle between the categories in that parent category. Hope it makes sense. Thx!
<?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>'; } ?>
September 10, 2020 at 3:22 am #1437762DavidStaffCustomer SupportHi there,
this user had a similar request:
https://generatepress.com/forums/topic/add-a-category-menu-to-product-pages/page/2/#post-1402255
See his questions and see my reply π
September 10, 2020 at 11:17 am #1438446FredrikHello David,
Ah! Thank you π This is very close to what I was looking for.
One thing that would make it better is that the category menu stays and not disappears when you click on a child category within the parent, to remove the need for a back button.
Let me know π
September 10, 2020 at 5:47 pm #1438699DavidStaffCustomer SupportTry this:
<?php // Setup get_terms product category $taxonomy = 'product_cat'; $current_term = get_queried_object(); $parent = $current_term->term_id; $children = get_term_children($parent, $taxonomy); $cat_args = array( 'parent' => $parent, 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true, 'depth' => 1 ); // If shop set top level only if ( is_shop() || empty($children) ) { $cat_args['parent'] = 0; } $product_categories = get_terms( $taxonomy , $cat_args ); // If either exists output terms if ( ! empty( $product_categories ) ) { echo '<ul class="woo-cat-nav">'; foreach ( $product_categories as $category ) { $term = get_term( $category, $taxonomy ); printf( '<li><a href="%1$s">%2$s</a></li>', get_term_link( $term ), $term->name ); } echo '</ul>'; } ?>
September 10, 2020 at 7:11 pm #1438753FredrikHello David,
Great π now the category menu follows.
But clicking a child menu item now will get you back to showing the top-level parent, instead of back to the children within the parent.
I think this prototype that I just did explains it better:
September 11, 2020 at 9:49 am #1439640DavidStaffCustomer SupportI know what your trying to achieve.
Unfortunately that particular piece of code just cannot do that – it would take Custom Development i am afraid.September 11, 2020 at 4:24 pm #1439968FredrikI figured so π no worries! I will try to figure it out somehow, also, is there any other way to go about this? Maybe it’s simply the wrong approach.
Thank you for all your help π
September 12, 2020 at 4:56 am #1440370DavidStaffCustomer SupportIts one of those WP challenges thats probably best asked on Stack Overflow if a paid developer is not an option.
It effectively requires two Loops of the get_terms – one for the parent level, and one for the child level. Which entails two separate queries. So we could do something like this:
<?php // Setup get_terms product category $taxonomy = 'product_cat'; $current_term = get_queried_object(); $parent = $current_term->term_id; $cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true, 'depth' => 1 ); // Build top level navigation $product_categories = get_terms( $taxonomy , array($cat_args, 'parent' => 0) ); build_cat_nav($product_categories); // If not top level ie. shop then build child terms navigation if( ! is_shop() ) { $product_categories = get_terms( $taxonomy , array($cat_args, 'parent' => $parent) ); build_cat_nav($product_categories); } // If either exists output terms function build_cat_nav($product_categories) { if ( ! empty( $product_categories ) ) { echo '<ul class="woo-cat-nav">'; foreach ( $product_categories as $category ) { $term = get_term( $category, $taxonomy ); printf( '<li><a href="%1$s">%2$s</a></li>', get_term_link( $term ), $term->name ); } echo '</ul>'; } } ?>
This will always display the top level terms.
If not on the shop page ie. where on a category, it will display any child terms.The issue comes when you then navigate to the childless category – getting it to display ITS parent categories is where it becomes a problem.
September 15, 2020 at 4:55 am #1443922FredrikHello David,
I’ll try asking over at Stack Overflow & check with a dev. Either way, if I come up with a solution I’ll post it here π
Thank you for your guidance & help!
September 15, 2020 at 6:17 am #1444014DavidStaffCustomer SupportAwesome – let us know π
-
AuthorPosts
- You must be logged in to reply to this topic.