Advertisement
Guest User

Wordpress walker that gets subpage's ordered by parent id

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