Help with show_option_all and show_option_none for taxonomy dropdown

I am using a drop down box (populated by custom taxonomies) to allow users to search CPTs.

Everything works fine except I can't get a "choose all" type option to work or a "select a TAXONOMY NAME" default option to show.

I am using get_terms_dropdown to populate the dropdown. I have successfully used show_option_all and show_option_none with wp_dropdown_categories so presumably it is just weak php skills that is the problem.

Below is my code - without show_option_all or show_option_none. I am hoping some can help me either:

1) integrate those two commands into my code, or
2) a different way to create a taxonomy dropdown

<form action="<?php bloginfo('url'); ?>" method="get">
	<div>
<?php
$taxonomies = array('TAXONOMY NAME');
$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="Näytä" /></div></noscript>
	</div></form>
<?php
function get_terms_dropdown($taxonomies, $args){
	$myterms = get_terms($taxonomies, $args);
	$output ="<select name='TAXONOMY SLUG'>";
	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;
}
?>
  • david10

    Thanks Kimberly

    I never had any luck using the wp_dropdown_categories syntax but I figured this out none the less.

    I am sure you noticed, but my code was a little more convoluted in order to get the taxonomy name, not the id to show in the dropdown box - this is a WP bug discussed here.

    I found my original work around code here.

    To get default ("select an option") text to show I added the following to my code.

    $output .="<option value='#'>ENTER YOUR DEFAULT TEXT HERE</option>";

    My complete code in case anyone needs it is below. This code will generate a dropdown box populated with custom taxonomies, listed by name, that will return all posts associated with taxonomy upon selection (no submit button).

    <form action="<?php bloginfo('url'); ?>" method="get">
    	<div>
    <?php
    $taxonomies = array('TAXONOMY NAME');
    $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="Näytä" /></div></noscript>
    	</div></form>
    <?php
    function get_terms_dropdown($taxonomies, $args){
    	$myterms = get_terms($taxonomies, $args);
    	$output ="<select name='TAXONOMY SLUG'>";
    	$output .="<option value='#'>ENTER YOUR DEFAULT TEXT HERE</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;
    }
    ?>