I am needing to create a custom taxonomy structure for my posts. I have read the WP Codex and am confident I can pull that off. My custom taxonomy will be hierarchical.
What I need to do is to create a filter based on this new taxonomy. I need to do this with drop down boxes.
Here is an example of my custom taxonomy will be:
- Artlcles
- Article 1
- Article 2
- Article 3
- Amendments
- Amendment 1
- Amendment 2
- Amendment 3
What I need is to have 2 drop down boxes. The first one will have all of the top level categories (Articles, Amendments, etc…) and then when one of those is selected it will populate/make appear a second drop down with all of the subcategories.
How can I achieve this?
It would not let me post this much code so I have to edit my original question….
This code below is what I have which is on this page http://www.constitutingamerica.org/dev2
<?php $args = array(
'show_option_all' => '',
'show_option_none' => '',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 1,
'hide_empty' => 0,
'child_of' => 0,
'parent' => 0,
'exclude' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 1,
'tab_index' => 0,
'taxonomy' => 'classification',
'hide_if_empty' => false,
'walker' => ''
);
$tax_menu_items = get_categories( $args ); ?>
<form name="class-filter" method="get" >
<select name="class-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value="">Select</option>
<? foreach ( $tax_menu_items as $tax_menu_item ): ?>
<option value="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>"><?php echo $tax_menu_item->name; ?></option>
<?php endforeach; ?>
</select>
<?php
$sub_tax_id = $wp_query->get_queried_object_id();
if(isset($sub_tax_id)) {
$args2 = array(
'show_option_all' => '',
'show_option_none' => '',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 1,
'hide_empty' => 0,
'child_of' => 0,
'parent' => $sub_tax_id,
'exclude' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 1,
'tab_index' => 0,
'taxonomy' => 'classification',
'hide_if_empty' => false,
'walker' => ''
);
$tax_menu_items2 = get_categories( $args2 ); ?>
<select name="class-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value="">Select</option>
<? foreach ( $tax_menu_items2 as $tax_menu_item2 ): ?>
<option value="<?php echo get_term_link($tax_menu_item2,$tax_menu_item2->taxonomy); ?>"><?php echo $tax_menu_item2->name; ?></option>
<?php endforeach; ?>
</select>
<? } ?>
</form>
I am so close. MY problem is that when the second box is selected it resets the second box to the same parent taxonomy as the first box. How can I get this second drop down to hold all of its original child taxonomies?
Enable categories for your Custom Post Type. You will want to hide the default category selecting meta box, and create your own.
For the code, you will want to retrieve all parent level categories and build your dropdown. Upon selecting a category, you will want to use AJAX to retrieve the list of categories for the selected parent.
Code for saving the CPT will have to look at your custom dropdowns to determine the actual category that it belongs to and appropriately display it back to the user.