I have a Contact Us page which is showing 9 locations on the sidebar by querying the custom post type “Distributors.” I want the user to be able to use a drop down that is populated by the taxonomy “Location.” For example, if the user selects “California” from the drop down, it will automatically update the list with just those posts tagged with California. What is the best way to go about this? Also, I don’t mind if the page refreshes when the user narrows down to their Country or State.
Ideally, I’d like the parent categories to be shown in one drop down, ie. Germany, Japan, USA, etc. Then a second drop down become populated with the children of that category. For example, the first drop down would display USA, then California would show up in the second drop down.
Thanks.
First use this code for populating parent dropdown.
You may need to use ajax/jquery to fill in the 2nd dropdown box.
jquery code would be something like this:
finally, process ajax in your functions.php file like this:
Hope it helps. Note: change the taxonomy names and other parameters/args/variables with respect to your needs.
Just wanted to share a link that helps explain comments contained in the answer:
http://solislab.com/blog/5-tips-for-using-ajax-in-wordpress/
Here’s tip 1 mentioned in one of the comments:
Although wp_localize_script() is created for localization, it also has another great use. You can declare javascript variables with namespaces to use with your script. Hereâs the syntax:
Hereâs how you should declare the URL to the file that handles AJAX (in this example, I use admin-ajax.php, which is discussed in tip #2):
This way, you wonât have to use PHP to print out JavaScript code, which is both ugly and non-cacheable. If you take a look at the generated HTML in the element, youâll find this:
Now, in your ajax.js file, you can use MyAjax.ajaxurl without having to resort to PHP and including wp-load.php. Please refer to tip #2 below for more about setting up the javascript code that sends the request, and how to handle the request properly.