- This topic has 30 replies, 6 voices, and was last updated 4 years, 7 months ago by
Leo.
-
AuthorPosts
-
August 7, 2020 at 11:13 am #1391875
Simon
Thank you David,
I made the change described to Woocommerce Shop Category Menu element successfully, and the code you kindly added creates the similar child category list perfectly. I now have two lists and wonder if it’s possible to display just the child categories when they exist and the main when they dont?
What do you think?
Thank you very much btw
August 7, 2020 at 2:47 pm #1392143David
StaffCustomer SupportTry this:
<?php // Setup get_X_terms args $cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true, ); $navClass = 'child'; // query child category $current_term = get_queried_object(); $parent = $current_term->term_id; $product_categories = get_term_children( $parent, 'product_cat', $cat_args ); // if no child get parent terms if ( empty($product_categories) ) { $navClass = 'parent'; $cat_args['parent'] = 0; $product_categories = get_terms( 'product_cat', $cat_args ); } // If either exists output terms if ( ! empty( $product_categories ) ) { echo '<ul class="woo-cat-nav ' . $navClass . '">'; 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>'; } ?>
August 10, 2020 at 2:00 am #1394917Simon
That works brilliantly, thank you!
August 10, 2020 at 3:49 am #1395076David
StaffCustomer SupportGlad to be of help
August 14, 2020 at 2:41 am #1402255Simon
Hi David,
Is there a way to limit this to only show the next level and not all hereditary levels? i.e. the parent shows the children of that category, but not the grandchildren (or great grandchildren)?
For example:
Shop -> shows Parent(s)
inside Parent cat(1) -> displays Children (cat1) only
inside Children -> displays Grandchildren only
inside Grandchildren -> Great Grandchildren onlyinside Parent cat(2) -> displays Children (cat2) only
I hope this makes sense! I can try and re-define if needed.
All the best
Simon
August 14, 2020 at 10:11 am #1403004David
StaffCustomer SupportGive this a try instead:
<?php // Setup get_terms product category $taxonomy = 'product_cat'; $current_term = get_queried_object(); $parent = $current_term->term_id; $cat_args = array( 'parent' => $parent, 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true, 'depth' => 1 ); // If shop set top level only if ( is_shop() ) { $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>'; } ?>
August 14, 2020 at 10:18 am #1403019Simon
Works perfectly, Thank you once more David!
August 14, 2020 at 10:37 am #1403052David
StaffCustomer SupportAwesome – glad to hear
March 4, 2021 at 3:34 am #1681555Rafał
David, this is great!
How could you polish the snippet for not throwing a PHP notice:
Notice: Undefined property: WP_Post_Type::$term_id in …\wp-content\plugins\gp-premium\elements\class-hooks.php(196) : eval()'d code on line 6
The notice falls out only at root, means Shop main page, but categories, and subcategories stay clean.March 4, 2021 at 7:19 am #1681984David
StaffCustomer SupportHI there,
is the Shop archive the front page of the site ?
March 4, 2021 at 12:23 pm #1682390Rafał
No, it isn’t.
March 5, 2021 at 3:54 am #1682985David
StaffCustomer SupportCan you raise a new topic and share a link to the site where i can see the error?
March 5, 2021 at 8:35 am #1683457Rafał
David, thank you for your efforts, but don’t make me raise a new topic, please…
You can see the notice here: http://prot3.rafiozoo.ovh/sklep/To be honest I don’t realize what weight is the issue.
I’m willing to set inwp-config.php
define('WP_DEBUG', false);
(possibly) Let’s make life easier 😉March 5, 2021 at 10:18 am #1683590Leo
StaffCustomer SupportHi Rafal,
Please open a new topic as David suggested since there are multiple authors in this thread already?
Thanks 🙂
March 6, 2021 at 4:47 am #1684272Rafał
Hi there!
If we set the condition earlier, then there’s no need to fetchterm_id
, which does not exist at Shop page, does it?<?php // Setup get_terms product category $taxonomy = 'product_cat'; $current_term = get_queried_object(); if ( is_shop() ) { $parent = 0; } else { $parent = $current_term->term_id; } $cat_args = array( 'parent' => $parent, 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true, 'depth' => 1 ); $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>'; } ?>
Anyway, works with no notice, and you are the best support on the web! Thank you!
-
AuthorPosts
- The topic ‘Add a category menu to product pages’ is closed to new replies.