php – wp_list_categories() – display first level categories and only current term's children in subcategory page-ThrowExceptions

Exception or error:

I’m using WordPress.

Have multiple categories with its subcategories. In general page, I’m displaying all first level categories. Here is my code:

$args = array(
   'type' => 'product-items',
   'child_of'  => 0,
   'parent'  => '',
   'order' => 'DESC',
   'hide_empty' => 0,
   'hierarchical' => 1,
   'exclude' => '',
   'include' => '',
   'number' => '',
   'taxonomy' => 'product-category',
   'pad_counts' => false,
   'depth' => 1,
   'title_li' => '' 
);
wp_list_categories($args);

Once you click and go inside a first level category, you need to see only its subcategories there. When I’m removing 'depth' => 1, option, all children appear under their parent category but for page speed/load, in sub page I need to show all first-level categories, but only current category’s children.

For example, I have below 3 categories:

  • Category 1
  • Category 2
  • Category 3

Imagine I click on “Category 1”. Now it is like this:

  • Category 1
    • 1st Sub Category of 1
    • 2nd Sub Category of 1
    • 3rd Sub Category of 1
  • Category 2
    • 1st Sub Category of 2
      • 1st sub of 2nd category sub
      • 2nd sub of 2nd category sub
      • 3rd sub of 2nd category sub
    • 2nd Sub Category of 2
    • 3rd Sub Category of 2
  • Category 3
    • 1st Sub Category of 3
    • 2nd Sub Category of 3
    • 3rd Sub Category of 3

But I need it to be like this in sub page:

  • Category 1
    • 1st Sub Category of 1
    • 2nd Sub Category of 1
    • 3rd Sub Category of 1
  • Category 2
  • Category 3

Not sure how to achieve this with wp_list_categories() function. Any ideas please?

How to solve:

It would be better if you use 2 get_terms() instead of wp_list_categories. It would be faster and customizable. One for parent categories, another one for children of current category. Here is working example:

   function display_cats($cats,$current=0,$current_children=array()){
    $ret= '<ul>';
    foreach ($cats as $cs){
      $children=($current!=$cs->term_id)?'':display_cats($current_children);
      $ret.= '<li> <a href="'.get_term_link($cs->term_id).'"> '.$cs->name.'</a> '.$children.' </li>
      ';
    }
    $ret.= '</ul>';
    return $ret;
  }


  $current_cat=9;//for example
  $parents=get_terms('product_cat',array('taxonomy'=>'product_cat','echo'=>false,'depth'=>0));
  $current_children=get_terms('product_cat',array('taxonomy'=>'product_cat','child_of'=>  $current_cat ,'echo'=>false));
  echo display_cats($parents,$current_cat,$current_children);

Answer:

I’d take the get_terms() path.
Something such as

$terms = get_terms($args);

foreach($terms as $term){
   // If $term is current term use get_terms() again to fetch its children
}

https://developer.wordpress.org/reference/functions/get_terms/

Answer:

For anyone who still needs help, here’s how to do it.

$category = get_queried_object();
$category_id = $category->term_id;

From this we will get the current category id and we need to pass it to the array.

'child_of'  => $category_id,

This will get you all the subcategories of the current category.

Hope this is helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *