Created
September 25, 2013 17:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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