Skip to main content
Added the image, removed "thank you" part and fixed the grammar.
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251

I have next category dropdown filter that atm supports only 2 leveltwo levels "parent -> Child" and iI want to get the third "parent -> child -> child".

I have 2two functions:

And it outputs next list: http://oi63.tinypic.com/2eyufrs.jpg (sorry for the link). Output

I tried severallseveral things to do, but nothing works at the momment.

I'll really apreciate your helpmoment.

Thank you!

I have next category dropdown filter that atm supports only 2 level "parent -> Child" and i want to get the third "parent -> child -> child".

I have 2 functions:

And it outputs next list: http://oi63.tinypic.com/2eyufrs.jpg (sorry for the link).

I tried severall things to do, but nothing works at the momment.

I'll really apreciate your help.

Thank you!

I have next category dropdown filter that atm supports only two levels "parent -> Child" and I want to get the third "parent -> child -> child".

I have two functions:

And it outputs next list: Output

I tried several things, but nothing works at the moment.

a piece of code was not well formatted.
Source Link
RRikesh
  • 5.7k
  • 4
  • 31
  • 45

function reviews_display_categories_filter( $ancestors = array(), $permalink = '', $parent = 0 ){ global $reviews_slugs; $categories = get_terms( 'review-category', array( 'hide_empty' => false, 'parent' => $parent ));

function reviews_display_categories_filter( $ancestors = array(), $permalink = '', $parent = 0 ){
    global $reviews_slugs;
    $categories = get_terms( 'review-category', array(
        'hide_empty' => false,
        'parent' => $parent
    ));

    if( !empty( $categories ) ){
        foreach( $categories as $category ){
            $option = '<option value="'.esc_attr( $category->slug ).'" '.( isset( $_GET[$reviews_slugs['review-category']] ) && $_GET[$reviews_slugs['review-category']] == $category->slug ? 'selected="selected"' : '' ).'>';
            $option .= $category->name.' ('.reviews_count_filter( $category ).')';
            $option .= '</option>';
            echo  $option;

            if( in_array( $category->term_id, $ancestors ) ){
                $children = get_term_children( $category->term_id, 'review-category' );
                reviews_display_category_children( $children, $ancestors, $permalink );
            }
        }
    }
}

function reviews_display_category_children( $children, $ancestors, $permalink, $depth = 2 ){
    global $reviews_slugs;
    if( !empty( $children ) ){
        foreach( $children as $child ){
            $term = get_term_by( 'id', $child, 'review-category' );

            $option = '<option value="'.esc_attr( $term->slug ).'" '.( isset( $_GET[$reviews_slugs['review-category']] ) && $_GET[$reviews_slugs['review-category']] == $term->slug ? 'selected="selected"' : '' ).'>';
            $option .= str_repeat('&nbsp;&nbsp;', $depth).$term->name.' ('.reviews_count_filter( $term ).')';
            $option .= '</option>';
            echo  $option;

            if( in_array( $child, $ancestors ) ){
                $children = get_term_children( $child, 'review-category' );
                reviews_display_category_children( $children, $ancestors, $permalink );
            }
        }
    }
}

function reviews_display_categories_filter( $ancestors = array(), $permalink = '', $parent = 0 ){ global $reviews_slugs; $categories = get_terms( 'review-category', array( 'hide_empty' => false, 'parent' => $parent ));

    if( !empty( $categories ) ){
        foreach( $categories as $category ){
            $option = '<option value="'.esc_attr( $category->slug ).'" '.( isset( $_GET[$reviews_slugs['review-category']] ) && $_GET[$reviews_slugs['review-category']] == $category->slug ? 'selected="selected"' : '' ).'>';
            $option .= $category->name.' ('.reviews_count_filter( $category ).')';
            $option .= '</option>';
            echo  $option;

            if( in_array( $category->term_id, $ancestors ) ){
                $children = get_term_children( $category->term_id, 'review-category' );
                reviews_display_category_children( $children, $ancestors, $permalink );
            }
        }
    }
}

function reviews_display_category_children( $children, $ancestors, $permalink, $depth = 2 ){
    global $reviews_slugs;
    if( !empty( $children ) ){
        foreach( $children as $child ){
            $term = get_term_by( 'id', $child, 'review-category' );

            $option = '<option value="'.esc_attr( $term->slug ).'" '.( isset( $_GET[$reviews_slugs['review-category']] ) && $_GET[$reviews_slugs['review-category']] == $term->slug ? 'selected="selected"' : '' ).'>';
            $option .= str_repeat('&nbsp;&nbsp;', $depth).$term->name.' ('.reviews_count_filter( $term ).')';
            $option .= '</option>';
            echo  $option;

            if( in_array( $child, $ancestors ) ){
                $children = get_term_children( $child, 'review-category' );
                reviews_display_category_children( $children, $ancestors, $permalink );
            }
        }
    }
}
function reviews_display_categories_filter( $ancestors = array(), $permalink = '', $parent = 0 ){
    global $reviews_slugs;
    $categories = get_terms( 'review-category', array(
        'hide_empty' => false,
        'parent' => $parent
    ));

    if( !empty( $categories ) ){
        foreach( $categories as $category ){
            $option = '<option value="'.esc_attr( $category->slug ).'" '.( isset( $_GET[$reviews_slugs['review-category']] ) && $_GET[$reviews_slugs['review-category']] == $category->slug ? 'selected="selected"' : '' ).'>';
            $option .= $category->name.' ('.reviews_count_filter( $category ).')';
            $option .= '</option>';
            echo  $option;

            if( in_array( $category->term_id, $ancestors ) ){
                $children = get_term_children( $category->term_id, 'review-category' );
                reviews_display_category_children( $children, $ancestors, $permalink );
            }
        }
    }
}

function reviews_display_category_children( $children, $ancestors, $permalink, $depth = 2 ){
    global $reviews_slugs;
    if( !empty( $children ) ){
        foreach( $children as $child ){
            $term = get_term_by( 'id', $child, 'review-category' );

            $option = '<option value="'.esc_attr( $term->slug ).'" '.( isset( $_GET[$reviews_slugs['review-category']] ) && $_GET[$reviews_slugs['review-category']] == $term->slug ? 'selected="selected"' : '' ).'>';
            $option .= str_repeat('&nbsp;&nbsp;', $depth).$term->name.' ('.reviews_count_filter( $term ).')';
            $option .= '</option>';
            echo  $option;

            if( in_array( $child, $ancestors ) ){
                $children = get_term_children( $child, 'review-category' );
                reviews_display_category_children( $children, $ancestors, $permalink );
            }
        }
    }
}
Source Link
Alex Grecu
  • 113
  • 1
  • 5

Filter By Category Dropdown List With Custom Taxonomies

I have next category dropdown filter that atm supports only 2 level "parent -> Child" and i want to get the third "parent -> child -> child".

I have 2 functions:

function reviews_display_categories_filter( $ancestors = array(), $permalink = '', $parent = 0 ){ global $reviews_slugs; $categories = get_terms( 'review-category', array( 'hide_empty' => false, 'parent' => $parent ));

    if( !empty( $categories ) ){
        foreach( $categories as $category ){
            $option = '<option value="'.esc_attr( $category->slug ).'" '.( isset( $_GET[$reviews_slugs['review-category']] ) && $_GET[$reviews_slugs['review-category']] == $category->slug ? 'selected="selected"' : '' ).'>';
            $option .= $category->name.' ('.reviews_count_filter( $category ).')';
            $option .= '</option>';
            echo  $option;

            if( in_array( $category->term_id, $ancestors ) ){
                $children = get_term_children( $category->term_id, 'review-category' );
                reviews_display_category_children( $children, $ancestors, $permalink );
            }
        }
    }
}

function reviews_display_category_children( $children, $ancestors, $permalink, $depth = 2 ){
    global $reviews_slugs;
    if( !empty( $children ) ){
        foreach( $children as $child ){
            $term = get_term_by( 'id', $child, 'review-category' );

            $option = '<option value="'.esc_attr( $term->slug ).'" '.( isset( $_GET[$reviews_slugs['review-category']] ) && $_GET[$reviews_slugs['review-category']] == $term->slug ? 'selected="selected"' : '' ).'>';
            $option .= str_repeat('&nbsp;&nbsp;', $depth).$term->name.' ('.reviews_count_filter( $term ).')';
            $option .= '</option>';
            echo  $option;

            if( in_array( $child, $ancestors ) ){
                $children = get_term_children( $child, 'review-category' );
                reviews_display_category_children( $children, $ancestors, $permalink );
            }
        }
    }
}

And it outputs next list: http://oi63.tinypic.com/2eyufrs.jpg (sorry for the link).

I want output like this:

Parent
-Child 1
--Child 2

I tried severall things to do, but nothing works at the momment.

I'll really apreciate your help.

Thank you!