Get subcategories after drop down box selection in wordpress

I currently have a dropdown box which contains a list of all of the different categories on my website, and I need to add a sub-categories box underneath it. So far, I have this code:

    wp_dropdown_categories('show_option_none='.__('Select one','appthemes').'&class=dropdownlist&orderby=name&order=ASC&hide_empty=0&hierarchical=1&taxonomy=ad_cat&depth=1&name=preselect');

   wp_dropdown_categories('show_option_none='.__('Select one','appthemes').'&class=dropdownlist&orderby=name&order=ASC&hide_empty=0&hierarchical=1&taxonomy=ad_cat&child_of=6');

The 2nd line of code finds all of the sub-categories that belong to the main category with the ID no. 6. How would I automatically change the no.6 when the first drop down box is changed? E.g. if I select ID 7 from the dropdown box, I need the subcategories for ID 7 to show instead of ID 6.

Read More

Thanks for any help

Edit: I am trying to run a function after one of the drop down options is chosen, could someone please tell me what I’m doing wrong?

$('#cat').click(function() {

alert("test");

});

Related posts

Leave a Reply

4 comments

  1. If you don’t have too many items you may want to filter client side instead of making wordpress run a new query everytime.

    You could also trigger the second dropdown by setting the ‘id’ attribute of the first dropdown’s rows to the number you wanted and on click/hover you can filter via the ID.

  2. <?php
                    $orderby = 'name';
                                $show_count = 0; // 1 for yes, 0 for no
                                $pad_counts = 0; // 1 for yes, 0 for no
                                $hierarchical = 1; // 1 for yes, 0 for no
                                $taxonomy = 'portfolio_category';
                                $title = '';
    
    
                        if( $terms = get_terms( array( 'child_of' => 39,
                                                  'orderby' => $orderby,
                                                  'show_count' => $show_count,
                                                  'pad_counts' => $pad_counts,
                                                  'hierarchical' => $hierarchical,
                                                  'taxonomy' => $taxonomy,
                                                  'depth'    => 2,
                                                  'hide_empty' => 0 ) ) ) : 
    
                            echo '<select class="techno-portfolio" name="categoryfilter"><option value="">Select technology</option>';
                            foreach ( $terms as $term ) :
                                echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
                            ;
                            endforeach;
                            echo '</select>';
                        endif;
                    ?>
    

    Use the above code to show the sub-category of main category inside drop-down the 39 is the category ID which subcategory you need to show in dropdown.