Skip to content

Instantly share code, notes, and snippets.

@hslaszlo
Last active August 29, 2015 14:21
Show Gist options
  • Save hslaszlo/afabcbc2a5dd52df8b83 to your computer and use it in GitHub Desktop.
Save hslaszlo/afabcbc2a5dd52df8b83 to your computer and use it in GitHub Desktop.
Hierarchical list of taxonomy terms
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment