0
votes

I am trying to get the product categories of subcategory of subcategory

<?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 );
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 ) {
      echo '<li class="title '.$cat->name.'"><a >
          '; 
      echo $cat->name ;
      echo '<i class="chevron right icon"></i>
        </a></li>';
      $sub_cats = "";
    }else {
      echo '<li class="titleVide '.$cat->name.'Hide"><a >
          '; 
      echo $cat->name ;
      echo '</a></li>';
      $sub_cats = "";
    }


      $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 '<li class="content"> <p>';
          foreach($sub_cats as $sub_category) {

            echo '
                <a href="'. get_term_link($sub_category->slug, "product_cat" ) .'">'. $sub_category->name . '</a>
             ';


          }

          echo ' </p></li>';
      } else {
    echo '';
      }

      $sub_cats = "";
  }       
} /* end foreach all_categories cat */

wp_reset_query();
?>

This code list all the top level categories and subcategories under them hierarchically, but i have subcategories of subcategory(sub-sub-category), so how can i list those sub subcategories(with clic).

Categories IN BO

List categories in FO

1

1 Answers

0
votes

try this below code by using child_of = current category id

<?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 );
    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 ) {
          echo '<li class="title '.$cat->name.'"><a >
              '; 
          echo $cat->name ;
          echo '<i class="chevron right icon"></i>
            </a></li>';
          $sub_cats = "";
        }else {
          echo '<li class="titleVide '.$cat->name.'Hide"><a >
              '; 
          echo $cat->name ;
          echo '</a></li>';
          $sub_cats = "";
        }


          $args2 = array(
                  'taxonomy'     => $taxonomy,
                  'child_of'     => $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 '<li class="content"> <p>';
              foreach($sub_cats as $sub_category) {

                echo '
                    <a href="'. get_term_link($sub_category->slug, "product_cat" ) .'">'. $sub_category->name . '</a>
                 ';


              }

              echo ' </p></li>';
          } else {
        echo '';
          }

          $sub_cats = "";
      }       
    } /* end foreach all_categories cat */

    wp_reset_query();

?>

or use wp_list_categories() function for listing category https://developer.wordpress.org/reference/functions/wp_list_categories/