3 * Taxonomy API: Walker_Category class
11 * Core class used to create an HTML list of categories.
17 class Walker_Category extends Walker {
20 * What the class handles.
26 * @see Walker::$tree_type
28 public $tree_type = 'category';
31 * Database fields to use.
37 * @see Walker::$db_fields
40 public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
43 * Starts the list before the elements are added.
48 * @see Walker::start_lvl()
50 * @param string $output Used to append additional content. Passed by reference.
51 * @param int $depth Optional. Depth of category. Used for tab indentation. Default 0.
52 * @param array $args Optional. An array of arguments. Will only append content if style argument
53 * value is 'list'. See wp_list_categories(). Default empty array.
55 public function start_lvl( &$output, $depth = 0, $args = array() ) {
56 if ( 'list' != $args['style'] )
59 $indent = str_repeat("\t", $depth);
60 $output .= "$indent<ul class='children'>\n";
64 * Ends the list of after the elements are added.
69 * @see Walker::end_lvl()
71 * @param string $output Used to append additional content. Passed by reference.
72 * @param int $depth Optional. Depth of category. Used for tab indentation. Default 0.
73 * @param array $args Optional. An array of arguments. Will only append content if style argument
74 * value is 'list'. See wp_list_categories(). Default empty array.
76 public function end_lvl( &$output, $depth = 0, $args = array() ) {
77 if ( 'list' != $args['style'] )
80 $indent = str_repeat("\t", $depth);
81 $output .= "$indent</ul>\n";
85 * Starts the element output.
90 * @see Walker::start_el()
92 * @param string $output Passed by reference. Used to append additional content.
93 * @param object $category Category data object.
94 * @param int $depth Optional. Depth of category in reference to parents. Default 0.
95 * @param array $args Optional. An array of arguments. See wp_list_categories(). Default empty array.
96 * @param int $id Optional. ID of the current category. Default 0.
98 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
99 /** This filter is documented in wp-includes/category-template.php */
100 $cat_name = apply_filters(
102 esc_attr( $category->name ),
106 // Don't generate an element if the category name is empty.
111 $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
112 if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
114 * Filters the category description for display.
118 * @param string $description Category description.
119 * @param object $category Category object.
121 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
125 $link .= $cat_name . '</a>';
127 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
130 if ( empty( $args['feed_image'] ) ) {
134 $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
136 if ( empty( $args['feed'] ) ) {
137 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
139 $alt = ' alt="' . $args['feed'] . '"';
140 $name = $args['feed'];
141 $link .= empty( $args['title'] ) ? '' : $args['title'];
146 if ( empty( $args['feed_image'] ) ) {
149 $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
153 if ( empty( $args['feed_image'] ) ) {
158 if ( ! empty( $args['show_count'] ) ) {
159 $link .= ' (' . number_format_i18n( $category->count ) . ')';
161 if ( 'list' == $args['style'] ) {
163 $css_classes = array(
165 'cat-item-' . $category->term_id,
168 if ( ! empty( $args['current_category'] ) ) {
169 // 'current_category' can be an array, so we use `get_terms()`.
170 $_current_terms = get_terms( $category->taxonomy, array(
171 'include' => $args['current_category'],
172 'hide_empty' => false,
175 foreach ( $_current_terms as $_current_term ) {
176 if ( $category->term_id == $_current_term->term_id ) {
177 $css_classes[] = 'current-cat';
178 } elseif ( $category->term_id == $_current_term->parent ) {
179 $css_classes[] = 'current-cat-parent';
181 while ( $_current_term->parent ) {
182 if ( $category->term_id == $_current_term->parent ) {
183 $css_classes[] = 'current-cat-ancestor';
186 $_current_term = get_term( $_current_term->parent, $category->taxonomy );
192 * Filters the list of CSS classes to include with each category in the list.
196 * @see wp_list_categories()
198 * @param array $css_classes An array of CSS classes to be applied to each list item.
199 * @param object $category Category data object.
200 * @param int $depth Depth of page, used for padding.
201 * @param array $args An array of wp_list_categories() arguments.
203 $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
205 $output .= ' class="' . $css_classes . '"';
206 $output .= ">$link\n";
207 } elseif ( isset( $args['separator'] ) ) {
208 $output .= "\t$link" . $args['separator'] . "\n";
210 $output .= "\t$link<br />\n";
215 * Ends the element output, if needed.
220 * @see Walker::end_el()
222 * @param string $output Passed by reference. Used to append additional content.
223 * @param object $page Not used.
224 * @param int $depth Optional. Depth of category. Not used.
225 * @param array $args Optional. An array of arguments. Only uses 'list' for whether should append
226 * to output. See wp_list_categories(). Default empty array.
228 public function end_el( &$output, $page, $depth = 0, $args = array() ) {
229 if ( 'list' != $args['style'] )
232 $output .= "</li>\n";