menu-walker.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Main menu walker
  4. *
  5. * @package vamtam/consulting
  6. */
  7. /**
  8. * Create HTML list of nav menu items.
  9. */
  10. class VamtamMenuWalker extends Walker_Nav_Menu {
  11. /**
  12. * Starts the list before the elements are added.
  13. *
  14. * @see Walker::start_lvl()
  15. *
  16. * @param string $output Passed by reference. Used to append additional content.
  17. * @param int $depth Depth of menu item. Used for padding.
  18. * @param array $args An array of arguments. @see wp_nav_menu()
  19. */
  20. function start_lvl( &$output, $depth = 0, $args = array() ) {
  21. $indent = str_repeat( "\t", $depth );
  22. $output .= "\n$indent<div class='sub-menu-wrapper'><ul class=\"sub-menu\">\n";
  23. }
  24. /**
  25. * Ends the list of after the elements are added.
  26. *
  27. * @see Walker::end_lvl()
  28. *
  29. * @param string $output Passed by reference. Used to append additional content.
  30. * @param int $depth Depth of menu item. Used for padding.
  31. * @param array $args An array of arguments. @see wp_nav_menu()
  32. */
  33. function end_lvl( &$output, $depth = 0, $args = array() ) {
  34. $indent = str_repeat( "\t", $depth );
  35. $output .= "$indent</ul></div>\n";
  36. }
  37. /**
  38. * Start the element output.
  39. *
  40. * @see Walker::start_el()
  41. *
  42. * @param string $output Passed by reference. Used to append additional content.
  43. * @param object $item Menu item data object.
  44. * @param int $depth Depth of menu item. Used for padding.
  45. * @param array $args An array of arguments. @see wp_nav_menu()
  46. * @param int $id Current item ID.
  47. */
  48. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  49. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  50. $class_names = $value = '';
  51. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  52. $classes[] = 'menu-item-' . $item->ID;
  53. /**
  54. * Filter the CSS class( es ) applied to a menu item's <li>.
  55. *
  56. * @since 3.0.0
  57. *
  58. * @param array $classes The CSS classes that are applied to the menu item's <li>.
  59. * @param object $item The current menu item.
  60. * @param array $args An array of arguments. @see wp_nav_menu()
  61. */
  62. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  63. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  64. /**
  65. * Filter the ID applied to a menu item's <li>.
  66. *
  67. * @param string The ID that is applied to the menu item's <li>.
  68. * @param object $item The current menu item.
  69. * @param array $args An array of arguments. @see wp_nav_menu()
  70. */
  71. $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
  72. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  73. $output .= $indent . '<li' . $id . $value . $class_names . '>';
  74. $atts = array();
  75. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  76. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  77. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  78. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  79. /**
  80. * Filter the HTML attributes applied to a menu item's <a>.
  81. *
  82. * @param array $atts {
  83. * The HTML attributes applied to the menu item's <a>, empty strings are ignored.
  84. *
  85. * @type string $title The title attribute.
  86. * @type string $target The target attribute.
  87. * @type string $rel The rel attribute.
  88. * @type string $href The href attribute.
  89. * }
  90. * @param object $item The current menu item.
  91. * @param array $args An array of arguments. @see wp_nav_menu()
  92. */
  93. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
  94. $attributes = '';
  95. foreach ( $atts as $attr => $value ) {
  96. if ( ! empty( $value ) ) {
  97. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  98. $attributes .= ' ' . $attr . '="' . $value . '"';
  99. }
  100. }
  101. $args = (object) $args;
  102. $item_output = $args->before;
  103. $item_output .= '<a' . $attributes . '>';
  104. /** This filter is documented in wp-includes/post-template.php */
  105. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  106. $item_output .= '</a>';
  107. $item_output .= $args->after;
  108. /**
  109. * Filter a menu item's starting output.
  110. *
  111. * The menu item's starting output only includes $args->before, the opening <a>,
  112. * the menu item's title, the closing </a>, and $args->after. Currently, there is
  113. * no filter for modifying the opening and closing <li> for a menu item.
  114. *
  115. * @param string $item_output The menu item's starting HTML output.
  116. * @param object $item Menu item data object.
  117. * @param int $depth Depth of menu item. Used for padding.
  118. * @param array $args An array of arguments. @see wp_nav_menu()
  119. */
  120. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  121. }
  122. /**
  123. * Ends the element output, if needed.
  124. *
  125. * @see Walker::end_el()
  126. *
  127. * @param string $output Passed by reference. Used to append additional content.
  128. * @param object $item Page data object. Not used.
  129. * @param int $depth Depth of page. Not Used.
  130. * @param array $args An array of arguments. @see wp_nav_menu()
  131. */
  132. function end_el( &$output, $item, $depth = 0, $args = array() ) {
  133. $output .= "</li>\n";
  134. }
  135. }