Right, seems a little empty here but I figured out the problem myself, again. The trick was to have empty values in the $output variables.
So
$output .="<option value="">Select taxonomy #1</option>";
NOT
$output .="<option value="#">Select taxonomy #1</option>";
This is how you can create two dropdown menus and filter out your posts using your custom taxonomies.
Paste this into your functions.php file
function get_terms_dropdown_grade_level($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name="MYTAXONOMY#1">"; //CHANGE ME!
$output .="<option value="">Select taxonomy #1</option>"; //CHANGE ME TO YOUR LIKING!
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;
}
function get_terms_dropdown_type($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$output ="<select name="MYTAXONOMY#2">"; //CHANGE ME!
$output .="<option value="">Select taxonomy #2</option>"; //CHANGE ME TO YOUR LIKING! 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;
}
Paste this onto the page you want the dropdown menus to appear. (I put mine on a special archives page, like archive-activities.php.)
<h3>Filter by:</h3>
<form action="<?php bloginfo('url'); ?>" method="get">
<div>
<?php
$taxonomies = array('MYTAXONOMY#1'); //CHANGE ME!
$args = array('orderby'=>'name','hide_empty'=>false);
$select = get_terms_dropdown_grade_level($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange="return this.form.submit()">", $select);
echo $select;
?>
<?php
$taxonomies = array('MYTAXONOMY#2'); //CHANGE ME!
$args = array('orderby'=>'name','hide_empty'=>false);
$select = get_terms_dropdown_type($taxonomies, $args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange="return this.form.submit()">", $select);
echo $select;
?>
<input type="submit" name="submit" value="filter" /> <!--CHANGE VALUE TO YOUR LIKING!-->
</div>
</form>
Cheers! 🙂