Ecommerce Shopify WordPress Discussion

WordPress: Show grand children categories – not just children categories

I have the following code: function woocommerce_subcats_from_parentcat_by_ID($category) { $parent_category_ID = 40; $args = array( 'hierarchical' => 1, 'show_option_none' => '', 'hide_empty' => 0, // Set to 0 to show empty categories and 1 to hide them 'parent' => $parent_category_ID, 'taxonomy' => 'product_cat' ); $subcategories = get_categories($args); echo '<ul class="woo_subcategory_list">'; foreach ($subcategories as $subcategory) { $link = get_term_link( $subcategory->slug, $subcategory->taxonomy ); echo '<li><a href="'. $link .'">'.$subcategory->name.'</a></li>'; } echo '</ul>'; } This works well, except it only show the children - not the grandchildren. ie Child 1 Grandchild 2 Grandchild 3 Child 2 Child 3 Only shows Child 1, Child 2, Child 3. How can I modify this code, so that it shows the grand children as well (in existing hierarchy)
To display both children and grandchildren categories in WordPress, you can modify the code by implementing a recursive function to iterate through the category hierarchy. Here's an updated version of your code: function woocommerce_subcats_from_parentcat_by_ID($category) { $parent_category_ID = 40; $args = array( 'hierarchical' => 1, 'show_option_none' => '', 'hide_empty' => 0, 'parent' => $parent_category_ID, 'taxonomy' => 'category' ); $subcategories = get_categories($args); echo '<ul class="woo_subcategory_list">'; foreach ($subcategories as $subcategory) { get_category_hierarchy($subcategory); } echo '</ul>'; } function get_category_hierarchy($category, $level = 0) { $link = get_term_link($category->slug, $category->taxonomy); echo '<li><a href="' . $link . '">' . $category->name . '</a></li>'; $args = array( 'hierarchical' => 1, 'show_option_none' => '', 'hide_empty' => 0, 'parent' => $category->term_id, 'taxonomy' => 'category' ); $children = get_categories($args); if (!empty($children)) { echo '<ul>'; foreach ($children as $child) { get_category_hierarchy($child, $level + 1); } echo '</ul>'; } } woocommerce_subcats_from_parentcat_by_ID(40); This should output a nested list structure with both children and grandchildren. Adjust the parameters as needed to fit your specific requirements.

December 30, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.