Skip to content

Instantly share code, notes, and snippets.

@luizventurote
Last active September 25, 2021 21:59
Show Gist options
  • Save luizventurote/10085105 to your computer and use it in GitHub Desktop.
Save luizventurote/10085105 to your computer and use it in GitHub Desktop.
Dropdown menu of categories with Walker.
<?php
# Walker Class
class my_Walker_CategoryDropdown extends Walker_CategoryDropdown {
function start_el(&$output, $category, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
$cat_name = apply_filters('list_cats', $category->name, $category);
$output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\"";
// Checks if the slug of the category is equal to the selected category
if ( $category->slug == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$output .= $pad.$cat_name;
if ( $args['show_count'] )
$output .= '&nbsp;&nbsp;('. $category->count .')';
if ( $args['show_last_update'] ) {
$format = 'Y-m-d';
$output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
}
$output .= "</option>\n";
}
}
# Arguments
$args = array(
'show_option_all' => '',
'show_option_none' => '',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'echo' => 1,
'selected' => '',
'hierarchical' => 0,
'name' => '',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
'walker' => new my_Walker_CategoryDropdown
);
# Dropdown
wp_dropdown_categories( $args );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment