我通过修改问题中链接的函数使其递归来解决这个问题。注释解释了我的更改:
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
我相信有一种更有效的方法可以做到这一点,而不是让函数每次都调用自己,但这是可行的。