Skip to content

Instantly share code, notes, and snippets.

@hamedmoody
Created December 1, 2019 05:47
Show Gist options
  • Save hamedmoody/8835d01afbeef80cbee647327e493bf8 to your computer and use it in GitHub Desktop.
Save hamedmoody/8835d01afbeef80cbee647327e493bf8 to your computer and use it in GitHub Desktop.
<?php
$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 ) );
wp_send_json( $terms );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment