Wordpress: hierarchical list of taxonomy terms

别等时光非礼了梦想. 提交于 2019-12-08 03:54:34

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/26732017/wordpress-hierarchical-list-of-taxonomy-terms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!