Advertisement
salvatorifabio

Custom Walker Nav Menu

Mar 3rd, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <?php
  2. if (! class_exists('custom_walker')) {
  3.     class custom_walker extends Walker_Nav_Menu {
  4.       function start_el(&$output, $item, $depth, $args) {
  5.            global $wp_query;
  6.            $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  7.  
  8.            $class_names = $value = '';
  9.  
  10.            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  11.            
  12.            $current_indicators = array('current-menu-item');
  13.    
  14.             $newClasses = array();
  15.            
  16.             foreach($classes as $el){
  17.                 //check if it's indicating the current page, otherwise we don't need the class
  18.                 if (in_array($el, $current_indicators)){
  19.                     array_push($newClasses, $el);
  20.                 }
  21.             }
  22.  
  23.            $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $newClasses ), $item ) );
  24.            if($class_names!='') $class_names = ' class="'. esc_attr( $class_names ) . '"';
  25.            //$class_names = ' class="'. esc_attr( $class_names ) . '"';
  26.            $output .= $indent . '<li' . $value . $class_names .'>';
  27.            
  28.  
  29.            $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
  30.            $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
  31.            $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
  32.            $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
  33.  
  34.            if($depth != 0)
  35.            {
  36.                      //children stuff
  37.            }
  38.  
  39.             $item_output = $args->before;
  40.             $item_output .= '<a'. $attributes .'>';
  41.             $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
  42.             $item_output .= '</a>';
  43.             $item_output .= $args->after;
  44.  
  45.             $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  46.             }
  47.     }   // end class
  48. } // end if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement