Skip to content

Instantly share code, notes, and snippets.

@marcelo-ribeiro
Created October 20, 2017 21:50
Show Gist options
  • Save marcelo-ribeiro/ffa0f9b7cdbb0696abf0ef4270422ccb to your computer and use it in GitHub Desktop.
Save marcelo-ribeiro/ffa0f9b7cdbb0696abf0ef4270422ccb to your computer and use it in GitHub Desktop.
List categories and subcategories in JSON format
$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