Skip to content

Instantly share code, notes, and snippets.

@eliasdorigoni
Created February 17, 2016 23:09
Show Gist options
  • Save eliasdorigoni/29d783c577fc8306c5d5 to your computer and use it in GitHub Desktop.
Save eliasdorigoni/29d783c577fc8306c5d5 to your computer and use it in GitHub Desktop.
Ejemplo de recursividad en taxonomias de WordPress
<?php
/**
* Arma recursivamente un arbol con las taxonomias jerárquicas.
* Devuelve un array.
* @param object $terms retorno de get_terms
* @return array
*/
function armarArbol($terms, $padre = 0, $profundidad = 0) {
if($profundidad >= 8) return '';
$retorno = array();
foreach ($terms as $i => $term) {
if ($term->parent == $padre) {
$retorno[$i] = (array) $term;
$retorno[$i]['children'] = armarArbol($terms, $term->term_id, $profundidad + 1);
}
}
return $retorno;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment