Created
August 27, 2014 14:45
-
-
Save roborourke/8bb9c70fa4051126d189 to your computer and use it in GitHub Desktop.
Category dropdown with links for wordpress
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 | |
/** | |
Usage: | |
wp_dropdown_categories(array( | |
'walker' => new Walker_CategoryDropdownLinks, | |
'taxonomy' => 'category' | |
)); | |
*/ | |
/** | |
* Create HTML dropdown list of Categories. | |
* | |
* @package WordPress | |
* @since 2.1.0 | |
* @uses Walker | |
*/ | |
class Walker_CategoryDropdownLinks extends Walker { | |
/** | |
* @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 ); | |
$link = get_term_link($category,$args['taxonomy']); | |
$output .= "\t<option class=\"level-$depth\" value=\"". $link ."\""; | |
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"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment