Last active
December 15, 2022 21:13
-
-
Save javierarques/22f2930e8906419ec154 to your computer and use it in GitHub Desktop.
Custom taxonomy dropdown walker
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
$(".goToCategory").change( function() { | |
if ( this.options[this.selectedIndex].value !== 0 ) { | |
location.href = this.options[this.selectedIndex].getAttribute('data-permalink'); | |
} | |
}); |
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
/** | |
* Create HTML dropdown list of Categories. | |
* | |
* @package WordPress | |
* @since 2.1.0 | |
* @uses Walker | |
*/ | |
class Walker_TaxonomyDropdown extends Walker_CategoryDropdown { | |
/** | |
* @see Walker::$tree_type | |
* @since 2.1.0 | |
* @var string | |
*/ | |
var $tree_type = 'category'; | |
/** | |
* @see Walker::$db_fields | |
* @since 2.1.0 | |
* @todo Decouple this | |
* @var array | |
*/ | |
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); | |
/** | |
* Start the element output. | |
* | |
* @see Walker::start_el() | |
* @since 2.1.0 | |
* | |
* @param string $output Passed by reference. Used to append additional content. | |
* @param object $category Category data object. | |
* @param int $depth Depth of category. Used for padding. | |
* @param array $args Uses 'selected' and 'show_count' keys, if they exist. @see wp_dropdown_categories() | |
*/ | |
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { | |
$pad = str_repeat(' ', $depth * 3); | |
/** This filter is documented in wp-includes/category-template.php */ | |
$cat_name = apply_filters( 'list_cats', $category->name, $category ); | |
$cat_permalink = get_term_link( $category ); | |
$output .= "\t<option data-permalink=\"".$cat_permalink."\" class=\"level-$depth\" value=\"".$category->name."\""; | |
if ( $category->term_id == $args['selected'] ) | |
$output .= ' selected="selected"'; | |
$output .= '>'; | |
$output .= $pad.$cat_name; | |
if ( $args['show_count'] ) | |
$output .= ' ('. number_format_i18n( $category->count ) .')'; | |
$output .= "</option>\n"; | |
} | |
} |
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
<?php wp_dropdown_categories( array( | |
'taxonomy' => 'ciudad', | |
'hide_empty' => false, | |
'hierarchical' => 1, | |
'show_option_all' => __('SELECCIONA TU GUÍA', 'theme'), | |
'walker' => new Walker_TaxonomyDropdown, | |
'class' => 'goToCategory', | |
'orderby' => 'name' | |
)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment