Skip to content

Instantly share code, notes, and snippets.

@amielucha
Created August 23, 2018 18:04
Show Gist options
  • Save amielucha/a9f4b33ced188a5e7923613a097cb143 to your computer and use it in GitHub Desktop.
Save amielucha/a9f4b33ced188a5e7923613a097cb143 to your computer and use it in GitHub Desktop.
WordPress Category Dropdown Nav
<?php
/**
* Category Dropdown. Can be used for listing categories on WP archive pages.
*/
?>
<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<option value="<?php echo get_permalink( get_option( 'page_for_posts' ) ) ?>">All</option>
<?php
$cats = get_categories();
$current_category = get_category( get_query_var( 'cat' ) );
foreach ($cats as $key => $cat)
echo '<option '. ( $cat->cat_ID === $current_category->cat_ID ? 'selected' : '' ) .' value="' . get_category_link( $cat->cat_ID ) . '">' . $cat->name . '</option>';
?>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment