woocommerce的子类别无法正确显示

时间:2017-08-26 12:53:06

标签: php wordpress plugins woocommerce

你能告诉我这段代码有什么问题吗?我可以查看类别,但子类别是类别的名称。

$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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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();

我在图片中宣传了这个问题。 提前谢谢

more described in the picture

1 个答案:

答案 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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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();