你可以使用下面的代码的所有Woocommerce类别和子类:
$taxonomy = 'product_cat';//Woocommerce taxanomy name
$orderby = 'name';
$show_count = 0; //set 1 for yes, 0 for no
$pad_counts = 0; //set 1 for yes, 0 for no
$hierarchical = 1; //set 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
);
//get all woocommerce categories on the basis of $args
$get_all_categories = get_categories($args);
foreach ($get_all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
//Create arguments for child category
$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
);
//Get child category
$sub_cats = get_categories($args2);
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
我希望这会帮助你。谢谢
你只想要父类别列表? –