我发现了如何做到这一点:
<?php
$taxonomy = \'product_cat\';
$orderby = \'name\';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = \'\';
$empty = 0;
$args = array(
\'taxonomy\' => $taxonomy,
\'orderby\' => $orderby,
\'show_count\' => $show_count,
\'pad_counts\' => $pad_counts,
\'hierarchical\' => $hierarchical,
\'title_li\' => $title,
\'hide_empty\' => $empty
);
?>
<?php $all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {?>
<?php $category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, \'thumbnail_id\', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo \'<h3 style="font-size:16px">\'. $cat->name .\'</h3>\';
$args2 = array(
\'taxonomy\' => $taxonomy,
\'child_of\' => 0,
\'parent\' => $category_id,
\'orderby\' => $orderby,
\'show_count\' => $show_count,
\'pad_counts\' => $pad_counts,
\'hierarchical\' => $hierarchical,
\'title_li\' => $title,
\'hide_empty\' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
if($sub_cats->$sub_category == 0) {
$thumbnail_id = get_woocommerce_term_meta( $sub_category->term_id, \'thumbnail_id\', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo \'<li style="list-style-type: none"><a href="\'. get_term_link($sub_category->slug, \'product_cat\') .\'"><img src="\'.$image.\'" width="45px" height="45px" style="margin-right:8px; margin-top:8px; float:left; " /></a></li>\';
}
}
echo \'</li>\';
}?>
<?php }
} ?>