How can I display parent and child taxonomies in separate drop downs?

The requirement is where i have a taxonomy State which is the parent and it has a child sub-taxonomies they are the Cities, Need to display the state as a drop down, when i select the particular state tax i have to display the corresponding child cities of the state tax in the second drop down…..kindly help me..

I found the solution for the above problem, but it works partial……..anyone with solution kindly help me….

Add the below code at the top of the template page after get_header(), as you can see i added a custom taxonomy ‘state’ in the wp_dropdown_categories….

<?php get_header(); ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script>
<script type="text/javascript">
$(function()
{
$('#main_cat').change(function()
{
    var $mainCat=$('#main_cat').val();

    // call ajax
    $("#sub_cat").empty();
    $.ajax
    (
        {
            url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",     
            type:'POST',
            data:'action=my_special_ajax_call&main_catid=' + $mainCat,

            success:function(results)
            {
                //  alert(results);
                $("#sub_cat").removeAttr("disabled");       
                $("#sub_cat").append(results);  
            }
        }
    );                                    
});
});               
<style type="text/css">
#content{width:auto; height:400px; margin:50px;}
</style>
<div id="content">
<?php 
 wp_dropdown_categories('show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Main Categories&name=main_cat&taxonomy=state');
?>
<select name="sub_cat" id="sub_cat" disabled="disabled"></select>
</div>
<?php
get_footer();
?>

Add the below code in functions.php file……..

function implement_ajax() {
if(isset($_POST['main_catid']))
    {
    $categories=  get_categories('child_of=".$_POST["main_catid'].'hide_empty=0'); 
      foreach ($categories as $cat) {
        $option .= '<option value="'.$cat->term_id.'">';
        $option .= $cat->cat_name;
        $option .= ' ('.$cat->category_count.')';
        $option .= '</option>';
      }
      echo '<option value="-1" selected="selected">Sub Categories</option>'.$option;
    die();
    } // end if
}
add_action('wp_ajax_my_special_ajax_call', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');//for users that are not logged in.

So now this is my structure….

State1

  • CT1-1
  • CT2-1
  • CT3-1

State2

  • CT1-2
  • CT2-2
  • CT3-2

State3

  • CT1-3
  • CT2-3
  • CT3-3

im able to display the parent taxonomies when i select the parent taxonomy the second drop gets activated but it does not populate the child items except it shows the static output ” Select “….. I’m not a Pro so kindly bare with me, Can anyone kindly resolve this issue

1 Answer
1

Hello Everyone out there, who are facing difficulty to display the parent and child taxonomies in drop down, i found the solution for the above problem……

edit the code in the functions.php file,

before the code was

$categories=  get_categories('child_of=".$_POST["main_catid'].'hide_empty=0');

now edit the code, like this…

$categories=  get_categories('child_of=".$_POST["main_catid'].'&hide_empty=0'.'&taxonomy=state');

and this works so beautifully, try it for sure……all i did was i concatenated the taxonomy, that’s all

cheers everyone, happy coding….. 🙂

Leave a Comment