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.
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");
});
You can do that with Javascript. Add a
onchange
attribute to the first dropdown and load the contents of the second dropdown via Ajax.Daniel you could use this tutorial to get you started:
http://www.1stwebdesigner.com/css/implement-ajax-wordpress-themes/
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.
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.