Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

251 Zeilen
7.7 KiB

  1. <?php
  2. /**
  3. * Nav Menu API: Walker_Nav_Menu class
  4. *
  5. * @package WordPress
  6. * @subpackage Nav_Menus
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used to implement an HTML list of nav menu items.
  11. *
  12. * @since 3.0.0
  13. *
  14. * @see Walker
  15. */
  16. class Walker_Nav_Menu extends Walker {
  17. /**
  18. * What the class handles.
  19. *
  20. * @since 3.0.0
  21. * @access public
  22. * @var string
  23. *
  24. * @see Walker::$tree_type
  25. */
  26. public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
  27. /**
  28. * Database fields to use.
  29. *
  30. * @since 3.0.0
  31. * @access public
  32. * @todo Decouple this.
  33. * @var array
  34. *
  35. * @see Walker::$db_fields
  36. */
  37. public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
  38. /**
  39. * Starts the list before the elements are added.
  40. *
  41. * @since 3.0.0
  42. *
  43. * @see Walker::start_lvl()
  44. *
  45. * @param string $output Passed by reference. Used to append additional content.
  46. * @param int $depth Depth of menu item. Used for padding.
  47. * @param stdClass $args An object of wp_nav_menu() arguments.
  48. */
  49. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  50. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  51. $t = '';
  52. $n = '';
  53. } else {
  54. $t = "\t";
  55. $n = "\n";
  56. }
  57. $indent = str_repeat( $t, $depth );
  58. $output .= "{$n}{$indent}<ul class=\"sub-menu\">{$n}";
  59. }
  60. /**
  61. * Ends the list of after the elements are added.
  62. *
  63. * @since 3.0.0
  64. *
  65. * @see Walker::end_lvl()
  66. *
  67. * @param string $output Passed by reference. Used to append additional content.
  68. * @param int $depth Depth of menu item. Used for padding.
  69. * @param stdClass $args An object of wp_nav_menu() arguments.
  70. */
  71. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  72. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  73. $t = '';
  74. $n = '';
  75. } else {
  76. $t = "\t";
  77. $n = "\n";
  78. }
  79. $indent = str_repeat( $t, $depth );
  80. $output .= "$indent</ul>{$n}";
  81. }
  82. /**
  83. * Starts the element output.
  84. *
  85. * @since 3.0.0
  86. * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
  87. *
  88. * @see Walker::start_el()
  89. *
  90. * @param string $output Passed by reference. Used to append additional content.
  91. * @param WP_Post $item Menu item data object.
  92. * @param int $depth Depth of menu item. Used for padding.
  93. * @param stdClass $args An object of wp_nav_menu() arguments.
  94. * @param int $id Current item ID.
  95. */
  96. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  97. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  98. $t = '';
  99. $n = '';
  100. } else {
  101. $t = "\t";
  102. $n = "\n";
  103. }
  104. $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
  105. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  106. $classes[] = 'menu-item-' . $item->ID;
  107. /**
  108. * Filters the arguments for a single nav menu item.
  109. *
  110. * @since 4.4.0
  111. *
  112. * @param stdClass $args An object of wp_nav_menu() arguments.
  113. * @param WP_Post $item Menu item data object.
  114. * @param int $depth Depth of menu item. Used for padding.
  115. */
  116. $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
  117. /**
  118. * Filters the CSS class(es) applied to a menu item's list item element.
  119. *
  120. * @since 3.0.0
  121. * @since 4.1.0 The `$depth` parameter was added.
  122. *
  123. * @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
  124. * @param WP_Post $item The current menu item.
  125. * @param stdClass $args An object of wp_nav_menu() arguments.
  126. * @param int $depth Depth of menu item. Used for padding.
  127. */
  128. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
  129. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  130. /**
  131. * Filters the ID applied to a menu item's list item element.
  132. *
  133. * @since 3.0.1
  134. * @since 4.1.0 The `$depth` parameter was added.
  135. *
  136. * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
  137. * @param WP_Post $item The current menu item.
  138. * @param stdClass $args An object of wp_nav_menu() arguments.
  139. * @param int $depth Depth of menu item. Used for padding.
  140. */
  141. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
  142. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  143. $output .= $indent . '<li' . $id . $class_names .'>';
  144. $atts = array();
  145. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  146. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  147. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  148. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  149. /**
  150. * Filters the HTML attributes applied to a menu item's anchor element.
  151. *
  152. * @since 3.6.0
  153. * @since 4.1.0 The `$depth` parameter was added.
  154. *
  155. * @param array $atts {
  156. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
  157. *
  158. * @type string $title Title attribute.
  159. * @type string $target Target attribute.
  160. * @type string $rel The rel attribute.
  161. * @type string $href The href attribute.
  162. * }
  163. * @param WP_Post $item The current menu item.
  164. * @param stdClass $args An object of wp_nav_menu() arguments.
  165. * @param int $depth Depth of menu item. Used for padding.
  166. */
  167. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
  168. $attributes = '';
  169. foreach ( $atts as $attr => $value ) {
  170. if ( ! empty( $value ) ) {
  171. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  172. $attributes .= ' ' . $attr . '="' . $value . '"';
  173. }
  174. }
  175. /** This filter is documented in wp-includes/post-template.php */
  176. $title = apply_filters( 'the_title', $item->title, $item->ID );
  177. /**
  178. * Filters a menu item's title.
  179. *
  180. * @since 4.4.0
  181. *
  182. * @param string $title The menu item's title.
  183. * @param WP_Post $item The current menu item.
  184. * @param stdClass $args An object of wp_nav_menu() arguments.
  185. * @param int $depth Depth of menu item. Used for padding.
  186. */
  187. $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
  188. $item_output = $args->before;
  189. $item_output .= '<a'. $attributes .'>';
  190. $item_output .= $args->link_before . $title . $args->link_after;
  191. $item_output .= '</a>';
  192. $item_output .= $args->after;
  193. /**
  194. * Filters a menu item's starting output.
  195. *
  196. * The menu item's starting output only includes `$args->before`, the opening `<a>`,
  197. * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
  198. * no filter for modifying the opening and closing `<li>` for a menu item.
  199. *
  200. * @since 3.0.0
  201. *
  202. * @param string $item_output The menu item's starting HTML output.
  203. * @param WP_Post $item Menu item data object.
  204. * @param int $depth Depth of menu item. Used for padding.
  205. * @param stdClass $args An object of wp_nav_menu() arguments.
  206. */
  207. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  208. }
  209. /**
  210. * Ends the element output, if needed.
  211. *
  212. * @since 3.0.0
  213. *
  214. * @see Walker::end_el()
  215. *
  216. * @param string $output Passed by reference. Used to append additional content.
  217. * @param WP_Post $item Page data object. Not used.
  218. * @param int $depth Depth of page. Not Used.
  219. * @param stdClass $args An object of wp_nav_menu() arguments.
  220. */
  221. public function end_el( &$output, $item, $depth = 0, $args = array() ) {
  222. if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  223. $t = '';
  224. $n = '';
  225. } else {
  226. $t = "\t";
  227. $n = "\n";
  228. }
  229. $output .= "</li>{$n}";
  230. }
  231. } // Walker_Nav_Menu