Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dcatkins40/4626395 to your computer and use it in GitHub Desktop.

Select an option

Save dcatkins40/4626395 to your computer and use it in GitHub Desktop.
Create a Wordpress dropdown using a function to change option values to custom taxonomies.
//Wordpress
/*functions.php
Input this function into the functions.php file to call when a dropdown is needed*/
function custom_taxonomy_dropdown( $taxonomy, $orderby = 'slug', $order = 'ASC', $limit = '-1', $name, $show_option_all = null, $show_option_none = null ) {
$args = array(
'orderby' => $orderby,
'order' => $order,
);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
printf( '<select name="%s" class="postform">', $name );
if ( $show_option_all ) {
printf( '<option value="0">%s</option>', $show_option_all );
}
if ( $show_option_none ) {
printf( '<option value="-1">%s</option>', $show_option_none );
}
foreach ( $terms as $term ) {
printf( '<option value="%s">%s</option>', $term->slug, $term->name );
}
print( '</select>' );
}
}
/*The call to create a custom taxonomy dropdown*/
<?php custom_taxonomy_dropdown($taxonomy, $orderby, $order, $limit, $name, $show_option_all, $show_option_none); ?>
/*Example jQuery to load dropdown option into DOM element*/
$('DOM_Element').bind('change', function(event) {
event.preventDefault;
var $pullTaxonomy = $(this).val(); //Pull the taxonomy from the value of the option selected
var $pullName = $(this).attr('name'); //Pull the taxonomy name from the select menu
var $pullURL = "<?php bloginfo('url'); ?>/" + $pullName + "/" + $pullTaxonomy;
if ($pullTaxonomy) {
$('Dynamic_Load_DOM_Element').load($pullURL);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment