Last active
February 24, 2017 14:55
-
-
Save Vyygir/4721998c02a3996a7aaf88f62f531e78 to your computer and use it in GitHub Desktop.
[WP] Custom taxonomy dropdown menu
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 | |
$dropdown_taxonomy = 'YOUR_TAXONOMY_NAME'; | |
$dropdown_terms = get_terms($dropdown_taxonomy); | |
$archive_url = home_url('/'); // change this to the URL the user should go to if they select "All" | |
if (!empty($dropdown_terms)) : | |
?> | |
<select id="term-dropdown" class="term-dropdown"> | |
<option value="<?php echo $archive_url; ?>">All</option> | |
<?php foreach ($dropdown_terms as $dropdown_term) : ?> | |
<option value="<?php echo get_term_link($dropdown_term); ?>" | |
<?php echo is_tax($dropdown_taxonomy, $dropdown_term->term_id) ? ' selected' : ''; ?>> | |
<?php echo $dropdown_term->name; ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
<script> | |
<!-- | |
var termSwitchDropdown = document.getElementById('term-dropdown'); | |
termSwitchDropdown.onchange = function() { | |
location.href = termSwitchDropdown.options[termSwitchDropdown.selectedIndex].value; | |
}; | |
--> | |
</script> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment