hermit

Custom walker with acceskeys (updated)

Apr 6th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 KB | None | 0 0
  1. class nav_walker extends Walker_Nav_Menu
  2. {
  3.       function start_el(&$output, $item, $depth, $args)
  4.       {
  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', 'current-menu-parent', 'current-menu-ancestor', 'current_page_item', 'current_page_parent', 'current_page_ancestor');
  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.            
  26.  
  27.            $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  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.            //accesskeys
  35.            global $wpdb;
  36.            global $access_keys;
  37.            $sql = $wpdb->get_results('SELECT term_taxonomy_id FROM '.$wpdb->term_relationships.' WHERE object_id = '.$item->db_id);
  38.            $menu_id = intval($sql[0]->term_taxonomy_id);
  39.            
  40.             if(count($access_keys[$menu_id])){
  41.                 if($access_keys[$menu_id][$item->ID] != ''){
  42.                     $attributes .= ' accesskey="'.$access_keys[$menu_id][$item->ID].'"';
  43.                 }
  44.             }
  45.            //end acceskeys
  46.  
  47.             $item_output = $args->before;
  48.             $item_output .= '<a'. $attributes .'>';
  49.             $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID );
  50.             $item_output .= '</a>';
  51.             $item_output .= $args->after;
  52.  
  53.             $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  54.            }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment