Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WP] Custom taxonomy dropdown menu
<?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
You can’t perform that action at this time.