php Woocommerce - 类别UL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Woocommerce - 类别UL相关的知识,希望对你有一定的参考价值。

<?php
$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 );
if( $all_categories ){
    echo '<ul>';
        foreach ($all_categories as $cat) {
           if($cat->category_parent == 0) {
               $category_id = $cat->term_id;
               echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name;

               $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) {
                   echo '<ul>';
                       foreach($sub_cats as $sub_category) {
                           echo  '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">' . $sub_category->name . '</a></li>';
                       }
                    echo '</ul>';
               }

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

?>

以上是关于php Woocommerce - 类别UL的主要内容,如果未能解决你的问题,请参考以下文章

php 获取产品类别儿童woocommerce

php WooCommerce:过滤产品类别列表

php WooCommerce |从类别中删除产品数量

php Woocommerce类别主菜单Dinamic

php woocommerce类别循环

php WooCommerce搜索产品按类别下拉列表