Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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