Skip to content

Instantly share code, notes, and snippets.

@kosinix
Last active December 19, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosinix/6019167 to your computer and use it in GitHub Desktop.
Save kosinix/6019167 to your computer and use it in GitHub Desktop.
Get taxonomy terms in hierarchichal format in WP
<?php
function get_taxonomy_tree($taxonomy, $parent=0){
$args = array(
'hide_empty'=>false,
'parent'=>$parent
);
$terms = get_terms( $taxonomy, $args );
if($terms){
$list=array();
foreach($terms as $term){
$holder = (array) $term; // Cast as array
$holder['children'] = get_taxonomy_tree($taxonomy, $term->term_id); // Call function recursively
$list[] = $holder;
}
return $list;
}
return array();
}
//Usage
print_r( get_taxonomy_tree('category') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment