I want to create a dynamic load for categories, for example, there is the “Countries” category, then “USA”, “Brazil”, “Spain” on a subcategory and “New York”, “Rio de Janeiro” and “Barcelona” at a sub-sub-category.
Like this
America [USA(New York), Brazil(Rio)] ; Europe [Spain(Barcelona)]
So I have 3 selects: First one the user would have to select “America” or “Europe”, then if I choose “America” the second one loads “Brazil” and “USA” for me to choose and so on.
What I’m trying is to use the wp_dropdown_categories
function and dynamically populate the next wp_dropdown_categories
choices, based on the current choice with the child_of
atribute.
I tried doing something with the javascript above to get the option chosen for the first one and it worked, but I can’t see a way to populate the other select dynamically, like making a wp_dropdown_categories
child_of, like this wp_dropdown_categories('child_of=the_category_i_have_chosen');
<script type="text/javascript">
var dropdown = document.getElementById("cat");
function onCatChange() {
var value = dropdown.options[dropdown.selectedIndex].value
}
}
dropdown.onchange = onCatChange;
</script>
I am using this for city / state select. “sehir” is custom taxonomy name.
header.php
functions.php
main category selectbox code:
sub-category select box:
you can improve in this way.