Loop Through Current Woocommerce Product Categories Sub Categories

I'm trying to loop through the current category's sub-categories but I'm getting a list of all sub-catego

I'm trying to loop through the current category's sub-categories but I'm getting a list of all sub-categories across the whole site including ones in other categories.

Here is my code:

  $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 );
 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;       

        $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) {
            foreach($sub_cats as $sub_category) { 

           echo $sub_category->name ;

            }   
        }
    }       
}

Does anyone know where I'm going wrong?

Thanks in advance,

James

I'm trying to loop through the current category's sub-categories but I'm getting a list of all sub-categories across the whole site including ones in other categories.

Here is my code:

  $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 );
 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;       

        $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) {
            foreach($sub_cats as $sub_category) { 

           echo $sub_category->name ;

            }   
        }
    }       
}

Does anyone know where I'm going wrong?

Thanks in advance,

James

Share Improve this question asked Feb 29, 2020 at 22:28 jamesmonk83jamesmonk83 31 bronze badge 1
  • with 'current' do you refer to a taxonomy archive page, or to a single product? – Michael Commented Mar 1, 2020 at 1:31
Add a comment  | 

1 Answer 1

Reset to default 0

You need to first fetch root level categories and then fetch their children in sub query. Your query will list only terms which is not expected output.

First get parent terms and then fetch their children and process them separately.

Simply use built in wordpress function get_terms() by passing taxonomy as product_cat and parent as your desired product category id. This will return sub categories of given parent product category.

There are functions like get_the_term_list(), the_terms() to help you out formatting such terms.

For more than one depth you csn define functions and go recursively.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744704345a4588963.html

相关推荐

  • Loop Through Current Woocommerce Product Categories Sub Categories

    I'm trying to loop through the current category's sub-categories but I'm getting a list of all sub-catego

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信