dimanche 1 mars 2015

Get direct children of custom taxonomy


I've googled extensively and tried my best, but I just can't get the first level children of the custom taxonomy in the code below to work! It only displays the parent level. What I am trying to achieve is the following:



Level 1 <--- at the moment, it only displays this level
Level 2 <-- I want to display this level too
Level 3 <-- but NOT this level


This is my code so far:



// get children of current parent.
// $tchildren = get_term_children($term->term_id, $taxonomy); //returns all children
$uchildren =get_terms( $taxonomy, array('hide_empty' => 0 , 'parent' => 1));

$children = array();
foreach ($uchildren as $child) {
$cterm = get_term_by( 'id', $child, $taxonomy );
// If include array set, exclude unless in array.
if ( is_array( $include ) && ! in_array( $cterm->slug, $include ) ) continue;
$children[$cterm->name] = $cterm;
}
ksort($children);

// OPTGROUP FOR PARENTS
if (count($children) > 0 ) {
if ($term->count > 0)
$list_of_terms .= '<option class ="group-result" value="'.$term->slug.'" '.$select.'>' . $term->name .' </option>';
} else
$list_of_terms .= '<option value="'.$term->slug.'" '.$select.'>'. $term->name .' </option>';

// now the CHILDREN.
foreach($children as $child) {
$select = ($current_selected == $child->slug) ? "selected" : ""; // Note: child, not cterm
$list_of_terms .= '<option class="result-sub" value="'.$child->slug.'" '.$select.'>'. $child->name.' </option>';

} //end foreach


If someone can help me with this beast, you'll be my freaken hero!





Aucun commentaire:

Enregistrer un commentaire