Skip to content

Instantly share code, notes, and snippets.

@Vyygir
Last active February 24, 2017 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vyygir/4721998c02a3996a7aaf88f62f531e78 to your computer and use it in GitHub Desktop.
Save Vyygir/4721998c02a3996a7aaf88f62f531e78 to your computer and use it in GitHub Desktop.
[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