Wordpress: hierarchical list of taxonomy terms

george picture george · Nov 4, 2014 · Viewed 12.4k times · Source

I am hitting a wall here, although it sounds pretty simple: I want to return a hierarchical list of custom post type taxonomy terms. What I get is the first level of terms and nested uls. But the sub terms are not showing. Any ideas?

Here's the code:

function return_terms_index() {
  $taxonomies = array( 
    'taxonomy_name',
  );

  $args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false, 
    'fields'            => 'all',
    'parent'            => 0,
    'hierarchical'      => true,
    'child_of'          => 0,
    'pad_counts'        => false,
    'cache_domain'      => 'core'    
  );

  $terms = get_terms($taxonomies, $args);

  $return .= '<ul>'; 

    foreach ( $terms as $term ) {

      // return terms (working)
      $return .= sprintf(
        '<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',       
        $term->term_id,
        $term->name,
        $term->description
      );

        $subterms = get_terms( array(
          'parent'   => $term->term_id,
          'hide_empty' => false
          ));

        $return .= '<ul>';

        foreach ( $subterms as $subterm ) {

          //return sub terms (not working :( )
          $return .= sprintf(
          '<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',       
          $subterm->term_id,
          $subterm->name,
          $subterm->description
          );

          $return .= '</li>'; //end subterms li
        }            

        $return .= '</ul>'; //end subterms ul

      $return .= '</li>'; //end terms li
    } //end foreach term

  $return .= '</ul>';

return $return;
}

Thanks!

Edit: here's the output.

<ul>
  <li id="category-176">
    1. <span class="post-count">0</span><span class="cat-description" style="display: none;">Description</span>
    <ul id="subTerm-176" style="display: block;"></ul>
  </li>
  <li id="category-49">
    2. <span class="post-count">0</span><span class="cat-description" style="display: none;">Langtitel/Beschreibung</span>
    <ul id="subTerm-49" style="display: none;"></ul>
  </li>
</ul>

Edit: taxonomies are returned in hierarchical list now, YAY! But I want to query and display posts of third level taxonomy terms as well and this bit of code doesn't do the trick.

$post_query = new WP_Query($taxonomies, array( 
  'term' => $subsubterm->term_id 
  )); ?>

  <?php if ( $post_query->have_posts() ) : 
  $return .= '<ul>';
    while ( $post_query->have_posts() ) : $post_query->the_post(); 
    $return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
    endwhile;
  $return .= '</ul>';

wp_reset_postdata();
else:
endif;

It has to be dynamic, so I can't specify a term by name/slug. But is this even possible?

'term' => $subsubterm->term_id

Thanks again.

Answer

Bhumi Shah picture Bhumi Shah · Nov 4, 2014

You have missed to pass $taxonomies in

 $subterms = get_terms($taxonomies, array(
      'parent'   => $term->term_id,
      'hide_empty' => false
      ));

Try following code

function return_terms_index() {
  $taxonomies = array( 
    'taxonomy_name',
  );

  $args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false, 
    'fields'            => 'all',
    'parent'            => 0,
    'hierarchical'      => true,
    'child_of'          => 0,
    'pad_counts'        => false,
    'cache_domain'      => 'core'    
  );

  $terms = get_terms($taxonomies, $args);

  $return .= '<ul>'; 

    foreach ( $terms as $term ) {

      // return terms (working)
      $return .= sprintf(
        '<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',       
        $term->term_id,
        $term->name,
        $term->description
      );

        $subterms = get_terms($taxonomies, array(
          'parent'   => $term->term_id,
          'hide_empty' => false
          ));

        $return .= '<ul>';

        foreach ( $subterms as $subterm ) {

          //return sub terms (not working :( )
          $return .= sprintf(
          '<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',       
          $subterm->term_id,
          $subterm->name,
          $subterm->description
          );

          $return .= '</li>'; //end subterms li
        }            

        $return .= '</ul>'; //end subterms ul

      $return .= '</li>'; //end terms li
    } //end foreach term

  $return .= '</ul>';

return $return;
}