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