• 9Oct
    2009

    WordPress Dropdown Categories

    I rarely use dropdown boxes to display data in WordPress. However, recently I needed to add this feature to a project I’m working on and I though I’d share.

    If your like me, whenever you need to do something in WordPress you have not done before the first place you check is the codex. I found some basic information on adding drop boxes, but they were all form based.  I was looking for something java oriented (I need java for future design considerations).

    I dug deeper online and found a few useful bits of code here and there, and managed to create a Frankenstein dropdown.

    Part of the  project I’m working on is a business directory. We are using categories and sub-categories to organize all the listings. One way to search the directory is by city. We have each city defined as a sub-category to a parent called geographic. This is important, because when I’m building my dropdown I need to display each child category separately.

    Here’s the code:

    <ul>
     <li id="categories">
     <?php wp_dropdown_categories('title_li=&hierarchical=0&show_count=1&child_of=9'); ?>
    
    <script type="text/javascript"><!--
     var dropdown = document.getElementById("cat");
     function onCatChange() {
     if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
     location.href = "<?php echo get_option('home');
    ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
     }
     }
     dropdown.onchange = onCatChange;
    --></script>
    </li>
    </ul>
    
     <!--<ul>
     <?php wp_list_categories('title_li=&hierarchical=0&show_count=1&child_of=9') ?>
     </ul>-->

    Notice I use the wp_dropdown_categories function and I call my sub category by using the child argument (child_of=9) the 9 representing the parent category.

    This is what it looks like

    wp_dropdown_screen

    That’s it, pretty simple! Hopefully you find this useful.

    4 Responses to WordPress Dropdown Categories

    1. Pingback: Brendan Sera-Shriar – A Lifestream» Daily Digest for October 10th

    2. Jake Macpherson
      October 13, 2009 at 1:21 pm

      i need your help, as you know there is that free them ‘irresistible’ i downloaded it but i have no idea how to put it into action on my actual blog itself, if i could be a pain, could you get back to me and tell me how to upload it on my blog as the theme i have just now is awful…thank you!

      Jake

      • webmaster
        October 14, 2009 at 1:40 pm

        Hey Jake,

        First you need to upload the theme to your theme directory yourdomain.com/wordpress/wp-content/themes/ then log into your admin, go to the appearance menu on the left and click on themes. Locate the theme you want to activate and click on activate. That’s all!

        You can find more info here http://codex.wordpress.org/Using_Themes

    3. Brandon
      October 27, 2009 at 12:25 am

      Is there anyway to have 3 drop down boxes? The first one pulling a list of main categories … and then once a category is chosen the next drop down will populate with the sub-categories from it, and the 3rd will do the same for the category above it? If you visit my site: http://demo.zoutman-designs.com/ you’ll see what I mean (look at the sidebar)

      I’ve been looking everywhere for a solution but haven’t found one yet.

    Leave a Reply

    Your email address will not be published. Required fields are marked *