zeshanshani
4/26/2017 - 9:46 AM

Get terms/categories HTML hierarchically. Update the HTML structure as you desire in function rarus_cat_list_item();

Get terms/categories HTML hierarchically. Update the HTML structure as you desire in function rarus_cat_list_item();

<?php 

/**
 * Recursively sort an array of taxonomy terms hierarchically. Child categories will be
 * placed under a 'children' member of their parent term.
 * @param Array   $cats     taxonomy term objects to sort
 * @param Array   $into     result array to put them in
 * @param integer $parentId the current parent ID to put them in
 * 
 * Source: https://wordpress.stackexchange.com/a/99516/111165
 */
function sort_terms_hierarchically( Array &$cats, Array &$into, $parentId = 0 ) {
  foreach ( $cats as $i => $cat ) {
    if ( $cat->parent == $parentId ) {
      $into[ $cat->term_id ] = $cat;
      unset( $cats[ $i ] );
    }
  }

  foreach ( $into as $topCat ) {
    $topCat->children = array();
    sort_terms_hierarchically( $cats, $topCat->children, $topCat->term_id );
  }
}

/**
 * Get Sidebar Category List Item
 */
function rarus_cat_list_item( $cat_el ) {

  $p_list = get_posts( array( 
    'posts_per_page' => -1,
    'post_type'      => 'products',
    'product_cat'    => $cat_el->slug
  ) );

  // Get Current Category for "current" Class
  $current_cats = get_the_terms( get_the_ID(), 'product_cat' );
  $current_cat = false;

  if ( is_array( $current_cats ) && ! empty( $current_cats ) ) {
    $current_cat = $current_cats[0]->term_id;
  }

  // Category Item Classes
  $classes = '';
  $classes .= ( $current_cat === $cat_el->term_id ) ? 'current' : '';
  $classes .= ( $cat_el->parent ) ? ' is-child' : '';

  ob_start();
  if ( $p_list ): ?>
    <li class="<?php echo $classes; ?>">
      <a href="<?php echo get_term_link( $cat_el ); ?>" class="opener">
      <?php echo $cat_el->name; ?> <i class="arrow icon-down_arrow"></i></a>
      <ul class="submenu">
      <?php foreach( $p_list as $p_el ): ?>
        <li><a href="<?php echo get_permalink( $p_el->ID ) ?>"><?php echo $p_el->post_title; ?></a></li>
      <?php endforeach; ?>
      </ul>
    </li>
  <?php endif;
  $result = ob_get_clean();

  return $result;
}

/**
 * Get Sidebar Category List
 * @param  array $cat_list  taxonomy terms
 * @return string           returns html of terms. 
 */
function rarus_get_term_list( $cat_list ) {
  $result = '';
  foreach( $cat_list as $cat_el ) {
    $result .= rarus_cat_list_item( $cat_el );
    if ( ! empty( $cat_el->children ) && is_array( $cat_el->children ) ) {
      $result .= rarus_get_term_list( $cat_el->children );
    }
  }
  return $result;
}
<?php

/**
 * change 'product_cat' to your desired taxonomy.
 */
$categories = get_terms( array( 
  'taxonomy'     => 'product_cat',
  'hierarchical' => false,
) );

$cat_list = array();
sort_terms_hierarchically( $categories, $cat_list );

if ( $cat_list ) {
  echo '<ul class="terms-list">';
  echo    sidebar_cat_list( $cat_list );
  echo '</ul>';
}

?>