Created
October 20, 2017 21:50
-
-
Save marcelo-ribeiro/ffa0f9b7cdbb0696abf0ef4270422ccb to your computer and use it in GitHub Desktop.
List categories and subcategories in JSON format
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
$args = [ | |
'taxonomy' => 'category', | |
'hide_empty' => 0, | |
'parent' => 0 | |
]; | |
function _get_child_terms( $items ) { | |
foreach ( $items as $item ) { | |
$item->children = get_terms( 'category', array( 'child_of' => $item->term_id, 'hide_empty' => 0 ) ); | |
if ( $item->children ) _get_child_terms( $item->children ); | |
} | |
return $items; | |
} | |
$terms = _get_child_terms( get_terms( $args ) ); | |
echo json_encode( $terms ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment