Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
//Código
$terms = get_categories(array(
'type' => 'represent_map',
'taxonomy' => 'represent_map_type')
);
$categories = array();
if (!empty($terms)) {
foreach ($terms as $t) {
if (0 == $t->parent) {
$categories[$t->term_id] = $t;
unset($terms[$t->term_id]);
}
}
}
foreach($terms as $cat) {
if ( !empty($cat->name) && !empty($cat->parent) ) {
$categories[$cat->parent]->children[] = $cat;
}
}
print_r($categories);
//Saída
/*
Array
(
[18] => stdClass Object
(
[term_id] => 18
[name] => Multiplataforma
[slug] => multiplataforma
[term_group] => 0
[term_taxonomy_id] => 18
[taxonomy] => represent_map_type
[description] =>
[parent] => 0
[count] => 1
[cat_ID] => 18
[category_count] => 1
[category_description] =>
[cat_name] => Multiplataforma
[category_nicename] => multiplataforma
[category_parent] => 0
)
[9] => stdClass Object
(
[term_id] => 9
[name] => Outdoor Tradicional
[slug] => outdoor-tradicional
[term_group] => 0
[term_taxonomy_id] => 9
[taxonomy] => represent_map_type
[description] =>
[parent] => 0
[count] => 1
[cat_ID] => 9
[category_count] => 1
[category_description] =>
[cat_name] => Outdoor Tradicional
[category_nicename] => outdoor-tradicional
[category_parent] => 0
[children] => Array
(
[0] => stdClass Object
(
[term_id] => 5
[name] => Cartaz 9x3
[slug] => cartaz-9x3
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => represent_map_type
[description] =>
[parent] => 9
[count] => 1
[cat_ID] => 5
[category_count] => 1
[category_description] =>
[cat_name] => Cartaz 9x3
[category_nicename] => cartaz-9x3
[category_parent] => 9
)
[1] => stdClass Object
(
[term_id] => 7
[name] => Lonado 9x3
[slug] => lonado-9x3
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => represent_map_type
[description] =>
[parent] => 9
[count] => 1
[cat_ID] => 7
[category_count] => 1
[category_description] =>
[cat_name] => Lonado 9x3
[category_nicename] => lonado-9x3
[category_parent] => 9
)
)
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment