Advertisement
Guest User

WordPress Clean Menu Walker

a guest
Apr 29th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 KB | None | 0 0
  1. <?php
  2. class My_Theme_Nav_Walker extends Walker_Nav_Menu {
  3.     // Find and replace the .current-xxx classe
  4.     function find_current( $classes ) {
  5.         return preg_match( '/(current[-_])|active|submenu/', $classes );
  6.     }
  7.  
  8.     // Create the <ul> submenu element
  9.     function start_lvl( &$output, $depth = 0, $args = array() ) {
  10.         $output .= "\n<ul class=\"sub-menu\">\n";
  11.     }
  12.  
  13.     // Create the <li> elements
  14.     function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  15.         $item_html = '';
  16.         parent::start_el( $item_html, $item, $depth, $args );
  17.  
  18.         if( $item->is_submenu && ( $depth === 0 ) ) :
  19.             $item_html = str_replace( '<a', '<a class="sub-item"', $item_html);
  20.         // <i class="fa fa-angle-down"> = Font Awesome icons.
  21.             $item_html = str_replace( '</a>', ' <i class="fa fa-angle-down"></i></a>', $item_html);
  22.         // I wonder if another method would be better
  23.         elseif( $item->is_submenu && ( $depth > 0 ) ) :
  24.             $item_html = str_replace( '<a', '<a class="sub-item"', $item_html);
  25.             $item_html = str_replace( '</a>', ' <i class="fa fa-angle-right"></i></a>', $item_html);
  26.         endif;
  27.  
  28.         $item_html = apply_filters( 'my_theme_menu_item', $item_html );
  29.         $output .= $item_html;
  30.     }
  31.  
  32.     // Display our elements
  33.     function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
  34.         $element->is_submenu = ( (!empty( $children_elements[$element->ID]) && (( $depth + 1) < $max_depth || ( $max_depth === 0))));
  35.  
  36.         if( $element->is_submenu) {
  37.             $element->classes[] = 'parent-menu';
  38.         }
  39.  
  40.         parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  41.     }
  42. }
  43.  
  44. function my_theme_menu_css_class( $classes, $item ) {
  45.     $slug      = sanitize_title( $item->title );
  46.     $classes   = preg_replace( '/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes );
  47.     $classes   = preg_replace( '/^((menu|page)[-_\w+]+)+/', '', $classes );
  48.     $classes[] = 'menu-' . $slug;
  49.     $classes   = array_unique( $classes );
  50.  
  51.     return array_filter( $classes );
  52. }
  53. add_filter('nav_menu_css_class', 'my_theme_menu_css_class', 10, 2);
  54. add_filter('nav_menu_item_id', '__return_null');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement