Skip to content

Instantly share code, notes, and snippets.

@Ollo
Created September 25, 2013 17:51
Show Gist options
  • Save Ollo/6703367 to your computer and use it in GitHub Desktop.
Save Ollo/6703367 to your computer and use it in GitHub Desktop.
Create a dropdown list without a button to navigate custom taxonomies for custom post types
<?php
// place in functions or a plugin
function cpt_taxonomy_dropdown($taxonomy) { ?>
<form action="/" method="get">
<select name="cat" id="cat" class="postform">
<option value="-1">Choose one...</option>
<?php
$terms = get_terms($taxonomy);
foreach ($terms as $term) {
printf( '<option class="level-0" value="%s">%s</option>', $term->slug, $term->name );
}
echo '</select></form>';
}
<?php
// call it and replace the TAXONOMY with slug of custom taxonomy
cpt_taxonomy_dropdown( 'TAXONOMY' ); ?>
<script type="text/javascript"><!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > '' ) {
location.href = "<?php echo get_option('home'); ?>/?TAXONOMY="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
--></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment