Wordpress: hierarchical list of taxonomy terms
13,650
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;
}
Author by
george
Updated on June 29, 2022Comments
-
george almost 2 years
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.
-
george over 9 yearsThanks for pointing that out! I implemented your code/missing $taxonomies, but the output stays the same (see edited post).
-
Bhumi Shah over 9 yearsCode is working fine.Check in your admin panel. Do you have any subterm with assigned post?
-
george over 9 yearsThanks so much, that did it! You're code works just fine now, don't know why it didn't in the first place.
-
george over 9 yearsOne problem solved, next one ahead: I want to query/display posts on subsubterm-level, but this code doesn't seem to work (see edit above). I even added the missing $taxonomies ;-)
-
Bhumi Shah over 9 yearsnot getting your query.can you add new question for new query?
-
george over 9 yearsThanks for getting back to me! The Query is at the end of my initial post (second edit). But I also added a new question here