Advertisement
SimeonGriggs

Pure.io CSS Menu Walker

Oct 10th, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. class Pure_Menu_Walker extends Walker {
  2.  
  3. var $db_fields = array('parent' => 'menu_item_parent', 'id' => 'db_id');
  4.  
  5. function start_lvl(&$output, $depth = 0, $args = array()) {
  6. $indent = str_repeat("", $depth);
  7. $output .= &quot;$indent<ul class=&#x27;pure-menu-children&#x27;>&quot;;
  8. }
  9.  
  10. function end_lvl(&$output, $depth = 0, $args = array()) {
  11. $indent = str_repeat(&quot;&quot;, $depth);
  12. $output .= &quot;$indent</ul>&quot;;
  13. }
  14.  
  15. function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
  16.  
  17. global $wp_query;
  18. $indent = ($depth) ? str_repeat(&quot;&quot;, $depth) : &#x27;&#x27;;
  19. $class_names = $value = &#x27;&#x27;;
  20. $classes = empty($item->classes) ? array() : (array) $item->classes;
  21.  
  22. /* Add active class */
  23. if (in_array(&#x27;current-menu-item&#x27;, $classes)) {
  24. $classes[] = &#x27;active&#x27;;
  25. unset($classes[&#x27;current-menu-item&#x27;]);
  26. }
  27.  
  28. /* Check for children */
  29. $children = get_posts(array(&#x27;post_type&#x27; => &#x27;nav_menu_item&#x27;, &#x27;nopaging&#x27; => true, &#x27;numberposts&#x27; => 1, &#x27;meta_key&#x27; => &#x27;_menu_item_menu_item_parent&#x27;, &#x27;meta_value&#x27; => $item->ID));
  30. if (!empty($children)) {
  31. $classes[] = &#x27;has-sub pure-menu-has-children pure-menu-allow-hover&#x27;;
  32. }
  33.  
  34. $class_names = join(&#x27; &#x27;, apply_filters(&#x27;nav_menu_css_class&#x27;, array_filter($classes), $item, $args));
  35. $class_names = $class_names ? &#x27; class=&quot;pure-menu-item &#x27; . esc_attr($class_names) . &#x27;&quot;&#x27; : &#x27;&#x27;;
  36.  
  37. $id = apply_filters(&#x27;nav_menu_item_id&#x27;, &#x27;menu-item-&#x27;. $item->ID, $item, $args);
  38. $id = $id ? &#x27; id=&quot;&#x27; . esc_attr($id) . &#x27;&quot;&#x27; : &#x27;&#x27;;
  39.  
  40. $output .= $indent . &#x27;<li&#x27; . $id . $value . $class_names .&#x27;>&#x27;;
  41.  
  42. $attributes = ! empty($item->attr_title) ? &#x27; title=&quot;&#x27; . esc_attr($item->attr_title) .&#x27;&quot;&#x27; : &#x27;&#x27;;
  43. $attributes .= ! empty($item->target) ? &#x27; target=&quot;&#x27; . esc_attr($item->target ) .&#x27;&quot;&#x27; : &#x27;&#x27;;
  44. $attributes .= ! empty($item->xfn) ? &#x27; rel=&quot;&#x27; . esc_attr($item->xfn ) .&#x27;&quot;&#x27; : &#x27;&#x27;;
  45. $attributes .= ! empty($item->url) ? &#x27; href=&quot;&#x27; . esc_attr($item->url ) .&#x27;&quot;&#x27; : &#x27;&#x27;;
  46.  
  47. $item_output = $args->before;
  48. $item_output .= &#x27;<a&#x27;. $attributes .&#x27; class=&quot;pure-menu-link&quot;>&#x27;;
  49. $item_output .= $args->link_before . apply_filters(&#x27;the_title&#x27;, $item->title, $item->ID) . $args->link_after;
  50. $item_output .= &#x27;</a>&#x27;;
  51. $item_output .= $args->after;
  52.  
  53. $output .= apply_filters(&#x27;walker_nav_menu_start_el&#x27;, $item_output, $item, $depth, $args);
  54. }
  55.  
  56. function end_el(&$output, $item, $depth = 0, $args = array()) {
  57. $output .= &quot;</li>&quot;;
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement