Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Created September 29, 2016 05:18
Show Gist options
  • Select an option

  • Save sandcastle/22b225b65bd00cc1bdca15f53856ddaa to your computer and use it in GitHub Desktop.

Select an option

Save sandcastle/22b225b65bd00cc1bdca15f53856ddaa to your computer and use it in GitHub Desktop.
A Product Sub-Category Dropdown
<?php
/**
* Shortcode for creating a dropdown for all brands.
*
* Doc:
* https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
*
* Inspiration:
* https://developer.wordpress.org/reference/functions/get_categories/
* https://gist.github.com/foxinni/2922611
*
* Usage:
* [product_brands_dropdown]
*
*/
add_shortcode( 'product_brands_dropdown', 'product_brands_dropdown' );
function product_brands_dropdown( $atts ) {
$select_id = 'brands-dropdown';
$args = array(
'id' => $select_id,
'show_option_all' => 'Select Brand',
'orderby' => 'Name',
'order' => 'ASC',
'child_of' => 1724, /* Brands Product Category */
'show_count' => true,
'taxonomy' => 'product_cat',
'value_field' => 'slug'
);
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('select#<?php echo $select_id; ?>').change(function(){
if (this.options[this.selectedIndex].text === 'Select Brand') {
return;
}
var slug = this.options[this.selectedIndex].value;
window.location.href = '<?php echo $url_base; ?>/product-category/brands/' + slug;
});
});
</script>
<?php
wp_dropdown_categories( $args );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment