Last active
August 29, 2015 14:27
-
-
Save mattsteele/9328ba6e6a68c5033180 to your computer and use it in GitHub Desktop.
Filter posts by category
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
<div class="styled-select"> | |
<?php $args = array( | |
'show_option_none' => __( 'Filter Blog By Category' ), | |
'orderby' => 'name', | |
'exclude' => 1, | |
); ?> | |
<?php wp_dropdown_categories( $args ); ?> | |
<script type="text/javascript"> | |
var dropdown = document.getElementById("cat"); | |
dropdown.addEventListener('change', function() { | |
if (dropdown.options[dropdown.selectedIndex].value > 0 ) | |
{ | |
location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value; | |
} | |
}); | |
</script> | |
</div> <!--/ styled-select --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If IE9+, another approach would be to use the
addEventListener()
to allow for an unlimited number ofonchange()
events and to prevent theonchange()
from being overwritten down the line: