Last active
December 19, 2015 21:19
-
-
Save kosinix/6019167 to your computer and use it in GitHub Desktop.
Get taxonomy terms in hierarchichal format in WP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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