Walker_Category

Core class used to create an HTML list of categories.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-walker-category.php  
  1. class Walker_Category extends Walker { 
  2.  
  3. /** 
  4. * What the class handles. 
  5. * @since 2.1.0 
  6. * @access public 
  7. * @var string 
  8. * @see Walker::$tree_type 
  9. */ 
  10. public $tree_type = 'category'; 
  11.  
  12. /** 
  13. * Database fields to use. 
  14. * @since 2.1.0 
  15. * @access public 
  16. * @var array 
  17. * @see Walker::$db_fields 
  18. * @todo Decouple this 
  19. */ 
  20. public $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); 
  21.  
  22. /** 
  23. * Starts the list before the elements are added. 
  24. * @since 2.1.0 
  25. * @access public 
  26. * @see Walker::start_lvl() 
  27. * @param string $output Used to append additional content. Passed by reference. 
  28. * @param int $depth Optional. Depth of category. Used for tab indentation. Default 0. 
  29. * @param array $args Optional. An array of arguments. Will only append content if style argument 
  30. * value is 'list'. See wp_list_categories(). Default empty array. 
  31. */ 
  32. public function start_lvl( &$output, $depth = 0, $args = array() ) { 
  33. if ( 'list' != $args['style'] ) 
  34. return; 
  35.  
  36. $indent = str_repeat("\t", $depth); 
  37. $output .= "$indent<ul class='children'>\n"; 
  38.  
  39. /** 
  40. * Ends the list of after the elements are added. 
  41. * @since 2.1.0 
  42. * @access public 
  43. * @see Walker::end_lvl() 
  44. * @param string $output Used to append additional content. Passed by reference. 
  45. * @param int $depth Optional. Depth of category. Used for tab indentation. Default 0. 
  46. * @param array $args Optional. An array of arguments. Will only append content if style argument 
  47. * value is 'list'. See wp_list_categories(). Default empty array. 
  48. */ 
  49. public function end_lvl( &$output, $depth = 0, $args = array() ) { 
  50. if ( 'list' != $args['style'] ) 
  51. return; 
  52.  
  53. $indent = str_repeat("\t", $depth); 
  54. $output .= "$indent</ul>\n"; 
  55.  
  56. /** 
  57. * Starts the element output. 
  58. * @since 2.1.0 
  59. * @access public 
  60. * @see Walker::start_el() 
  61. * @param string $output Passed by reference. Used to append additional content. 
  62. * @param object $category Category data object. 
  63. * @param int $depth Optional. Depth of category in reference to parents. Default 0. 
  64. * @param array $args Optional. An array of arguments. See wp_list_categories(). Default empty array. 
  65. * @param int $id Optional. ID of the current category. Default 0. 
  66. */ 
  67. public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { 
  68. /** This filter is documented in wp-includes/category-template.php */ 
  69. $cat_name = apply_filters( 
  70. 'list_cats',  
  71. esc_attr( $category->name ),  
  72. $category 
  73. ); 
  74.  
  75. // Don't generate an element if the category name is empty. 
  76. if ( ! $cat_name ) { 
  77. return; 
  78.  
  79. $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" '; 
  80. if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) { 
  81. /** 
  82. * Filters the category description for display. 
  83. * @since 1.2.0 
  84. * @param string $description Category description. 
  85. * @param object $category Category object. 
  86. */ 
  87. $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"'; 
  88.  
  89. $link .= '>'; 
  90. $link .= $cat_name . '</a>'; 
  91.  
  92. if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) { 
  93. $link .= ' '; 
  94.  
  95. if ( empty( $args['feed_image'] ) ) { 
  96. $link .= '('; 
  97.  
  98. $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"'; 
  99.  
  100. if ( empty( $args['feed'] ) ) { 
  101. $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"'; 
  102. } else { 
  103. $alt = ' alt="' . $args['feed'] . '"'; 
  104. $name = $args['feed']; 
  105. $link .= empty( $args['title'] ) ? '' : $args['title']; 
  106.  
  107. $link .= '>'; 
  108.  
  109. if ( empty( $args['feed_image'] ) ) { 
  110. $link .= $name; 
  111. } else { 
  112. $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />'; 
  113. $link .= '</a>'; 
  114.  
  115. if ( empty( $args['feed_image'] ) ) { 
  116. $link .= ')'; 
  117.  
  118. if ( ! empty( $args['show_count'] ) ) { 
  119. $link .= ' (' . number_format_i18n( $category->count ) . ')'; 
  120. if ( 'list' == $args['style'] ) { 
  121. $output .= "\t<li"; 
  122. $css_classes = array( 
  123. 'cat-item',  
  124. 'cat-item-' . $category->term_id,  
  125. ); 
  126.  
  127. if ( ! empty( $args['current_category'] ) ) { 
  128. // 'current_category' can be an array, so we use `get_terms()`. 
  129. $_current_terms = get_terms( $category->taxonomy, array( 
  130. 'include' => $args['current_category'],  
  131. 'hide_empty' => false,  
  132. ) ); 
  133.  
  134. foreach ( $_current_terms as $_current_term ) { 
  135. if ( $category->term_id == $_current_term->term_id ) { 
  136. $css_classes[] = 'current-cat'; 
  137. } elseif ( $category->term_id == $_current_term->parent ) { 
  138. $css_classes[] = 'current-cat-parent'; 
  139. while ( $_current_term->parent ) { 
  140. if ( $category->term_id == $_current_term->parent ) { 
  141. $css_classes[] = 'current-cat-ancestor'; 
  142. break; 
  143. $_current_term = get_term( $_current_term->parent, $category->taxonomy ); 
  144.  
  145. /** 
  146. * Filters the list of CSS classes to include with each category in the list. 
  147. * @since 4.2.0 
  148. * @see wp_list_categories() 
  149. * @param array $css_classes An array of CSS classes to be applied to each list item. 
  150. * @param object $category Category data object. 
  151. * @param int $depth Depth of page, used for padding. 
  152. * @param array $args An array of wp_list_categories() arguments. 
  153. */ 
  154. $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) ); 
  155.  
  156. $output .= ' class="' . $css_classes . '"'; 
  157. $output .= ">$link\n"; 
  158. } elseif ( isset( $args['separator'] ) ) { 
  159. $output .= "\t$link" . $args['separator'] . "\n"; 
  160. } else { 
  161. $output .= "\t$link<br />\n"; 
  162.  
  163. /** 
  164. * Ends the element output, if needed. 
  165. * @since 2.1.0 
  166. * @access public 
  167. * @see Walker::end_el() 
  168. * @param string $output Passed by reference. Used to append additional content. 
  169. * @param object $page Not used. 
  170. * @param int $depth Optional. Depth of category. Not used. 
  171. * @param array $args Optional. An array of arguments. Only uses 'list' for whether should append 
  172. * to output. See wp_list_categories(). Default empty array. 
  173. */ 
  174. public function end_el( &$output, $page, $depth = 0, $args = array() ) { 
  175. if ( 'list' != $args['style'] ) 
  176. return; 
  177.  
  178. $output .= "</li>\n"; 
  179.