Category dropdown script issue
-
Hello, I am using a modified snippet to display my post categories in a dropdown. I am able to display the categories in a dropdown as expected, but the script is broken, as there is no action when a category is selected. Can someone help to see what I may be missing?
Thank you.
add_shortcode( 'tax_dropdown', 'taxonomy_dropdown' ); function taxonomy_dropdown( $atts ) { // Attributes $atts = shortcode_atts( array( 'hide_empty' => '1', // or '0' 'show_count' => '0', // or '0' 'orderby' => 'name', // or 'order' 'taxonomy' => 'post_tag', ), $atts, 'tax_dropdown' ); global $wp_query; $taxonomy = $atts['taxonomy']; $taxonomy_name = get_taxonomy( $taxonomy )->labels->singular_name; ob_start(); wp_dropdown_categories( array( 'hide_empty' => $atts['hide_empty'], 'show_count' => $atts['show_count'], 'orderby' => $atts['orderby'], 'selected' => isset( $wp_query->query_vars[$taxonomy] ) ? $wp_query->query_vars[$taxonomy] : '', 'show_option_none' => sprintf( __( 'Select a %s', 'post' ), $taxonomy_name ), 'option_none_value' => '', 'value_field' => 'slug', 'taxonomy' => $taxonomy, 'name' => $taxonomy, 'class' => 'dropdown_'.$taxonomy, ) ); ?> <script type='text/javascript'> jQuery(function($){ var select = '.dropdown_tag', taxonomy = '<?php echo $taxonomy; ?>'; function onTaxChange() { if ( $(select).val() !=='' ) { location.href = '<?php echo esc_url( home_url() ); ?>/?'+taxonomy+'='+$(select).val(); } } $(select).change( onTaxChange ); }); </script> <?php return ob_get_clean(); }
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Category dropdown script issue’ is closed to new replies.