/custom_nav_walker.php

https://gitlab.com/dhimanbarua/WP-ThemeComet · PHP · 243 lines · 65 code · 28 blank · 150 comment · 6 complexity · 794bb0f92776abf092013028af4b622e MD5 · raw file

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