Here is a quick example that will help you create a drop down menu, populated with all of the terms from a custom taxonomy.
This code is mostly from this thread on WordPress.com forums, I’ve just changed a few things for my situation.
Here’s the function that does most of the hard work.
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name='buscategory'>";
$output .="<option value='#'>Please select a business category</option>";
foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
Then place this code where you would like to call the drop down menu, this could be using a hook or in your template files.
<form action="<?php bloginfo('url'); ?>" method="get">
<p>Or search for a service from the drop down menu:</p>
<?php
$taxonomies = array('buscategory');
$args = array('orderby'=>'name','hide_empty'=>true);
$select = get_terms_dropdown($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><div><input type="submit" value="Go" /></div></noscript>
</form>
</div>
You’ll need to update the name of the taxonomy for this to work, you’ll see in the above example I’ve used ‘buscategory’ it needs to be update in both code blocks.
Since learning about Custom Post Types in WordPress 3.x I seem to be using them in just about every WordPress build we do.
Recent discussion