3 * Taxonomy API: Walker_Category class
11 * Core class used to create an HTML list of categories.
17 class Walker_Category extends Walker {
19 * What the class handles.
21 * @see Walker::$tree_type
25 public $tree_type = 'category';
28 * Database fields to use.
30 * @see Walker::$db_fields
35 public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
38 * Starts the list before the elements are added.
40 * @see Walker::start_lvl()
44 * @param string $output Passed by reference. Used to append additional content.
45 * @param int $depth Depth of category. Used for tab indentation.
46 * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
47 * @see wp_list_categories()
49 public function start_lvl( &$output, $depth = 0, $args = array() ) {
50 if ( 'list' != $args['style'] )
53 $indent = str_repeat("\t", $depth);
54 $output .= "$indent<ul class='children'>\n";
58 * Ends the list of after the elements are added.
60 * @see Walker::end_lvl()
64 * @param string $output Passed by reference. Used to append additional content.
65 * @param int $depth Depth of category. Used for tab indentation.
66 * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
67 * @wsee wp_list_categories()
69 public function end_lvl( &$output, $depth = 0, $args = array() ) {
70 if ( 'list' != $args['style'] )
73 $indent = str_repeat("\t", $depth);
74 $output .= "$indent</ul>\n";
78 * Start the element output.
80 * @see Walker::start_el()
84 * @param string $output Passed by reference. Used to append additional content.
85 * @param object $category Category data object.
86 * @param int $depth Depth of category in reference to parents. Default 0.
87 * @param array $args An array of arguments. @see wp_list_categories()
88 * @param int $id ID of the current category.
90 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
91 /** This filter is documented in wp-includes/category-template.php */
92 $cat_name = apply_filters(
94 esc_attr( $category->name ),
98 // Don't generate an element if the category name is empty.
103 $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
104 if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
106 * Filter the category description for display.
110 * @param string $description Category description.
111 * @param object $category Category object.
113 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
117 $link .= $cat_name . '</a>';
119 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
122 if ( empty( $args['feed_image'] ) ) {
126 $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
128 if ( empty( $args['feed'] ) ) {
129 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
131 $alt = ' alt="' . $args['feed'] . '"';
132 $name = $args['feed'];
133 $link .= empty( $args['title'] ) ? '' : $args['title'];
138 if ( empty( $args['feed_image'] ) ) {
141 $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
145 if ( empty( $args['feed_image'] ) ) {
150 if ( ! empty( $args['show_count'] ) ) {
151 $link .= ' (' . number_format_i18n( $category->count ) . ')';
153 if ( 'list' == $args['style'] ) {
155 $css_classes = array(
157 'cat-item-' . $category->term_id,
160 if ( ! empty( $args['current_category'] ) ) {
161 // 'current_category' can be an array, so we use `get_terms()`.
162 $_current_terms = get_terms( $category->taxonomy, array(
163 'include' => $args['current_category'],
164 'hide_empty' => false,
167 foreach ( $_current_terms as $_current_term ) {
168 if ( $category->term_id == $_current_term->term_id ) {
169 $css_classes[] = 'current-cat';
170 } elseif ( $category->term_id == $_current_term->parent ) {
171 $css_classes[] = 'current-cat-parent';
177 * Filter the list of CSS classes to include with each category in the list.
181 * @see wp_list_categories()
183 * @param array $css_classes An array of CSS classes to be applied to each list item.
184 * @param object $category Category data object.
185 * @param int $depth Depth of page, used for padding.
186 * @param array $args An array of wp_list_categories() arguments.
188 $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
190 $output .= ' class="' . $css_classes . '"';
191 $output .= ">$link\n";
192 } elseif ( isset( $args['separator'] ) ) {
193 $output .= "\t$link" . $args['separator'] . "\n";
195 $output .= "\t$link<br />\n";
200 * Ends the element output, if needed.
202 * @see Walker::end_el()
206 * @param string $output Passed by reference. Used to append additional content.
207 * @param object $page Not used.
208 * @param int $depth Depth of category. Not used.
209 * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories()
211 public function end_el( &$output, $page, $depth = 0, $args = array() ) {
212 if ( 'list' != $args['style'] )
215 $output .= "</li>\n";