显示woocommerce类别列表

时间:2016-11-30 06:37:32

标签: woocommerce categories

如何在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类别替换它。

4 个答案:

答案 0 :(得分:1)

  

WooCommerce产品类别被视为product_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;
}

答案 1 :(得分:0)

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

  $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 ;
            }   
        }
    }       
}

我希望它会对你有所帮助。感谢

答案 2 :(得分:0)

$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>';
 }

答案 3 :(得分:0)

转到 WooCommerce>设置,选择产品标签,然后选择显示选项。对于商店页面显示默认类别显示中的每个选项,选择同时显示

function woocommerce_product_category( $args = array() ) {
    $woocommerce_category_id = get_queried_object_id();
  $args = array(
        'parent' => $woocommerce_category_id
  );
  $terms = get_terms( 'product_cat', $args );
  if ( $terms ) {
        echo '<ul class="woocommerce-categories">';
        foreach ( $terms as $term ) {
            echo '<li class="woocommerce-product-category-page">';
            woocommerce_subcategory_thumbnail( $term );
            echo '<h2>';
            echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
            echo $term->name;
            echo '</a>';
            echo '</h2>';
            echo '</li>';
        }
        echo '</ul>';
  }
}
add_action( 'woocommerce_before_shop_loop', 'woocommerce_product_category', 100 );

因此,有一个woocommerce_product_category()函数可在运行输出产品的循环之前输出类别或子类别。

这是List Subcategories of a WooCommerce Product Category

的完整指南