Created
January 7, 2019 13:41
-
-
Save alexfurr/f77b128584bf0e57e4fffe3f8cb46a84 to your computer and use it in GitHub Desktop.
Creates multidimensional array of all categories in a WP blog
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
$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
Taken from https://stackoverflow.com/questions/6865207/php-wordpress-get-a-multidimensional-array-of-category-objects