0

I have a dropdown showing a custom taxonomy with multiple levels (children). Below is the code for it. Can someone please explain how I can limit the children shown? For instance, at the moment it shows:

Parent -> Child -> Child of Child

I only want it to show:

Parent -> Child

Thanks! :)

function my_dropdown_categories( $taxonomy, $current_selected = '', $include = null ) {
// Get all terms of the chosen taxonomy
$terms = get_terms($taxonomy, array('orderby' => 'name'));

// our content variable
$list_of_terms = '<select id="location" data-placeholder="Choose your area..."  class="selectboxSingle" name="location">';

if ( ! is_wp_error( $terms ) ) foreach($terms as $term){

    // If include array set, exclude unless in array.
    if ( is_array( $include ) && ! in_array( $term->slug, $include ) ) continue;

    $select = ($current_selected == $term->slug) ? "selected" : ""; // Note: ==

    if ($term->parent == 0 ) {

        // get children of current parent.
        $tchildren = get_term_children($term->term_id, $taxonomy);

        $children = array();
        foreach ($tchildren 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 ) {
        //     $list_of_terms .= '<optgroup label="'. $term->name .'">';
             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>';
        //$i++;

        // now the CHILDREN.

        foreach($children as $child) {
          //   $select = ($current_selected == $child->slug) ? "selected" : ""; // Note: child, not cterm
             $list_of_terms .= '<option value="'.$child->slug.'" '.$select.'>'. $child->name.' </option>';
        } //end foreach

        if (count($children) > 0 ) {
            $list_of_terms .= "</optgroup>";
        }
    }
}

$list_of_terms .= '</select>';

return $list_of_terms;
}
1

get_term_children() is by purpose recursive, it will always get all children and is not configurable.

get_terms() is more generic function for terms retrieval. Using parent argument for it will only retrieve one level of children (while child_of will retrieve all). See linked documentation in Codex for details.

1
  • Something like this? $tchildren = get_terms($taxonomy,array('parent' => get_queried_object_id(),)); I'm not sure how to implement it properly, since the dropdown is dynamically populated, and that code is a bit over my head :/ – Guit4eva Aug 31 '14 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.