Skip to content

Instantly share code, notes, and snippets.

@alexfurr
Created January 7, 2019 13:41
Show Gist options
  • Save alexfurr/f77b128584bf0e57e4fffe3f8cb46a84 to your computer and use it in GitHub Desktop.
Save alexfurr/f77b128584bf0e57e4fffe3f8cb46a84 to your computer and use it in GitHub Desktop.
Creates multidimensional array of all categories in a WP blog
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
) );
$cats_tree = get_cat_tree(0,$categories);
function get_cat_tree($parent,$categories) {
$result = array();
foreach($categories as $category){
if ($parent == $category->category_parent) {
$category->children = get_cat_tree($category->cat_ID,$categories);
$result[] = $category;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment