Woocommerce类别层次结构

时间:2018-09-10 05:11:40

标签: wordpress loops woocommerce

如何在woocommerce中显示类别层次结构

我想要的是这样的东西

给了这个 domain.com/category/balloons/helium-baloons 层次结构将是:

  • 气球
    • Helium Baloons
  • 装饰
  • 婚礼

为简化想法,它应该是这样的

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以将 wp_list_categories 函数与参数一起使用

wp_list_categories(
    array(
        'taxonomy'=>product_cat'
    )
);

答案 1 :(得分:0)

使用以下代码实现此功能

$myterms = get_terms( 'category', array( 'parent' => 0 ) );
$current_term = get_queried_object();
echo '<ul>';
foreach($myterms as $term){ 
    echo '<li> <a href="'.get_term_link($term->term_id).'">'.$term->name.'</a>';
    if($term->term_id == $current_term->parent){
        $child_terms = get_terms( 'category', array('child_of'=>$term->term_id) );
        if($child_terms){
            echo '<ul>';
                foreach($child_terms as $child_term){
                    echo '<li> <a href="'.get_term_link($child_term->term_id).'">'.$child_term->name.'</a>';
                }
            echo '</ul>';

        }
    }
    echo '</li>';   
}
echo '</ul>';

如果您的子继承人很大,也可以使此代码递归。

相关问题