你能告诉我这段代码有什么问题吗?我可以查看类别,但子类别是类别的名称。
$orderby = 'name';
$empty = 0;
$args = array(
'orderby' => $orderby,
'show_count' => $show_count,
'hide_empty' => $empty,
);
$all_categories = get_terms( 'product_cat', $args );
$html = '';
if (count($all_categories))
{
$array = $all_categories;
foreach($all_categories as $category)
{
if($category->parent == 0)
{
$html .= '<div class="checkbox"><label><input type="checkbox" class="product-cageories" value="'.$category->term_id.'"> '.$category->name.'</label></div>';
$j = 0;
foreach($array as $index => $child)
{
if($child->parent == $category->term_id)
{
$html .= '<div class="checkbox"><span class="pull-left"> </span><label><input type="checkbox" class="product-cageories" value="'.$category->term_id.'"> '.$category->name.'</label></div>';
unset($array[$index]);
}
if($child->term_id == $category->term_id)
{
unset($array[$index]);
}
}
}
else
{
continue;
}
}
}
echo $html;
wp_die();
我在图片中宣传了这个问题。 提前谢谢
答案 0 :(得分:0)
这是解决方案 您已经打印了类别,而不是子类别。
$orderby = 'name';
$empty = 0;
$args = array( 'orderby' => $orderby, 'show_count' => $show_count,
'hide_empty' => $empty, );
$all_categories = get_terms( 'product_cat', $args );
$html = '';
if (count($all_categories)) {
$array = $all_categories;
foreach($all_categories as $category) {
if($category->parent == 0) {
$html .= '<div class="checkbox"><label><input type="checkbox" class="product-cageories" value="'.$category->term_id.'"> '.$category->name.'</label></div>';
$j = 0;
foreach($array as $index => $child) {
if($child->parent == $category->term_id) {
$html .= '<div class="checkbox"><span class="pull-left"> </span><label><input type="checkbox" class="product-cageories" value="'.$child->term_id.'"> '.$child->name.'</label></div>';
unset($array[$index]);
}
if($child->term_id == $category->term_id) {
unset($array[$index]);
}
}
}else{
continue;
}
}
}
echo $html;
wp_die();