$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WooCommerce - Display nested list of all subcategories on archive-product.php|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WooCommerce - Display nested list of all subcategories on archive-product.php

matteradmin7PV0评论

When a user clicks on a product category from the WooCommerce shop page, instead of the default, I need it to look like the below design, the second picture marks what I mean:

So basically, I need to iterate through each subcategory, display it, check it it has subcategories, and, if it does, display them. I'm currently able to get the sub-subcategory level using the answer from this post and hooking it to "woocommerce_after_subcategory":

I just need help checking to see if those subcategories have a subcategory and then displaying them.

Please let me know if I need to further explain something! Thanks!

When a user clicks on a product category from the WooCommerce shop page, instead of the default, I need it to look like the below design, the second picture marks what I mean:

So basically, I need to iterate through each subcategory, display it, check it it has subcategories, and, if it does, display them. I'm currently able to get the sub-subcategory level using the answer from this post and hooking it to "woocommerce_after_subcategory":

https://wordpress.stackexchange/a/101273

I just need help checking to see if those subcategories have a subcategory and then displaying them.

Please let me know if I need to further explain something! Thanks!

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jan 26, 2016 at 19:16 asdfasdf 211 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I solved this by modifying the function linked to in the question to make it recursive. The comments explain my changes:

function woocommerce_subcats_from_parentcat_by_ID( $parent_cat_ID ) {
$args = array(
   'hierarchical' => 1,
   'show_option_none' => '',
   'hide_empty' => 0,
   'parent' => $parent_cat_ID,
   'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);

if ( $subcats ) { // added if statement to check if there are subcategories
  echo '<ul class="product-category-list">';
    foreach ($subcats as $sc) {
      $link = get_term_link( $sc->slug, $sc->taxonomy );
        echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
        woocommerce_subcats_from_parentcat_by_ID( $sc->term_id ); // function calls itself
    }
  echo '</ul>';
} else {
  return; //return if no subcategories
    }} // last bracket kept being pushed out of code block if I used a line break

I'm sure there is a more efficient way to do this instead of having the function call itself every time but this works.

Post a comment

comment list (0)

  1. No comments so far