我试图循环浏览当前类别的子类别,但我得到了整个网站所有子类别的列表,包括其他类别中的子类别。
这是我的代码:
$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
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
$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) {
echo $sub_category->name ;
}
}
}
}
有人知道我错在哪里吗?
提前感谢,
詹姆斯