Woocommerce 显示电子商务类别列表

Woocommerce 显示电子商务类别列表,woocommerce,categories,Woocommerce,Categories,如何获取woocommerce中的类别列表? 使用此代码,我可以获得wordpress类别列表: function gaga_lite_category_lists(){ $categories = get_categories( array( 'hide_empty' => 0, 'exclude' => 1 ) ); $category_lists = array(); $cate

如何获取woocommerce中的类别列表? 使用此代码,我可以获得wordpress类别列表:

function gaga_lite_category_lists(){
    $categories = get_categories(
        array(
            'hide_empty' => 0,
            'exclude' => 1
        )
    );


$category_lists = array();
$category_lists[0] = __('Select Category', 'gaga-lite');
foreach($categories as $category) :
    $category_lists[$category->term_id] = $category->name;
endforeach;
return $category_lists;

}
我想用woocommerce类别替换它

WooCommerce产品类别被视为
Product\u cat
taxonomy

这是代码

function gaga_lite_category_lists()
{
    $category_lists = array();
    $category_lists[0] = __('Select Category', 'gaga-lite');
    $args = array(
        'taxonomy' => 'product_cat',
        'orderby' => 'name',
        'hierarchical' => 0, // 1 for yes, 0 for no  
        'hide_empty' => 0,
        'exclude' => 1 //list of product_cat id that you want to exclude (string/array).
    );
    $all_categories = get_categories($args);
    foreach ($all_categories as $cat)
    {
        if ($cat->category_parent == 0)
        {
            $category_lists[$cat->term_id] = $cat->name;
            //get_term_link($cat->slug, 'product_cat')
        }
    }
    return $category_lists;
}

您可以使用以下代码获取所有类别和子类别:

  $taxonomy     = 'product_cat';//Woocommerce taxanomy name
  $orderby      = 'name';  
  $show_count   = 0;      //set 1 for yes, 0 for no
  $pad_counts   = 0;      //set 1 for yes, 0 for no
  $hierarchical = 1;      //set 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
  );

 //get all woocommerce categories on the basis of $args  
 $get_all_categories = get_categories( $args );
 foreach ($get_all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;       
        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

       //Create arguments for child category
        $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
        );

        //Get child category
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  $sub_category->name ;
            }   
        }
    }       
}
$taxonomy='product_cat'//我的名字是什么
$orderby='name';
$show_count=0//设置1表示是,设置0表示否
$pad_计数=0//设置1表示是,设置0表示否
$hierarchical=1//设置1表示是,设置0表示否
$title='';
$empty=0;
$args=数组(
“分类法”=>$taxonomy,
'orderby'=>$orderby,
'show_count'=>$show_count,
“pad_计数”=>$pad_计数,
“分层”=>$hierarchical,
“title_li”=>$title,
“hide_empty”=>$empty
);
//以$args为基础获取所有woocommerce类别
$get_all_categories=get_categories($args);
foreach($cat获取所有类别){
如果($cat->category\u parent==0){
$category\u id=$cat->term\u id;
回声“
”; //为子类别创建参数 $args2=数组( “分类法”=>$taxonomy, 'child_of'=>0, “父项”=>$category\u id, 'orderby'=>$orderby, 'show_count'=>$show_count, “pad_计数”=>$pad_计数, “分层”=>$hierarchical, “title_li”=>$title, “hide_empty”=>$empty ); //获取子类别 $sub_cats=get_categories($args2); 如果($sub_猫){ foreach($sub_猫作为$sub_类别){ echo$sub_category->name; } } } }
我希望它能帮助你。谢谢

$orderby='name';
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
 $cat_args = array(
'orderby'    => $orderby,
'order'      => $order,
'hide_empty' => $hide_empty,
 );

 $product_categories = get_terms( 'product_cat', $cat_args );

 if( !empty($product_categories) ){
 echo '<ul>';
     foreach ($product_categories as $key => $category) {
          echo '<li>';
          echo '<a href="'.get_term_link($category).'" >';
          echo $category->name;
          echo '</a>';
          echo '</li>';
      }
 echo '</ul>';
 }
$order='asc'; $hide_empty=false; $cat_args=数组( 'orderby'=>$orderby, “订单”=>$order, 'hide_empty'=>$hide_empty, ); $product\U CATEGORES=获取术语('product\U cat',$cat\U参数); 如果(!空($product_categories)){ 回声“
    ”; foreach($key=>$category的产品类别){ 回音“
  • ”; 回声'; 回音“
  • ”; } 回声“
”; }
您只想要父类别列表?不鼓励只使用代码的答案,因为它们不会为将来的读者提供太多信息。请对您所写的内容提供一些解释