Advertisement
Guest User

Extending Walker_Category Class

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