Advertisement
Palitux

Custom Walker to extract current sub-menu

May 23rd, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.58 KB | None | 0 0
  1. /**
  2.  * Custom Walker to extract current sub-menu
  3.  *
  4.  * @since xx
  5.  * @uses Walker
  6.  */
  7. class Custom_Walker_Nav_Sub_Menu extends Walker_Nav_Menu {
  8.  
  9.     var $found_parents = array();
  10.  
  11.     /**
  12.      * Start the element output.
  13.      *
  14.      * The $args parameter holds additional values that may be used with the child
  15.      * class methods. Includes the element output also.
  16.      *
  17.      * @since 2.1.0
  18.      * @abstract
  19.      *
  20.      * @param string $output            Passed by reference. Used to append additional content.
  21.      * @param object $object            The data object.
  22.      * @param int    $depth             Depth of the item.
  23.      * @param array  $args              An array of additional arguments.
  24.      * @param int    $current_object_id ID of the current item.
  25.      */
  26.     function start_el( &$output, $item, $depth, $args ) {
  27.  
  28.         global $wp_query;
  29.  
  30.         //this only works for second level sub navigations
  31.         $parent_item_id = 0;
  32.  
  33.         $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  34.  
  35.         $class_names = $value = '';
  36.  
  37.         $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  38.  
  39.         $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  40.         $class_names = ' class="' . esc_attr( $class_names ) . '"';
  41.  
  42.  
  43.         // Checks if the current element is in the current selection (it is menu sublevel)
  44.         if ( strpos( $class_names, 'current-menu-item' ) || strpos( $class_names, 'current-menu-parent' ) || strpos( $class_names, 'current-menu-ancestor' ) || ( is_array( $this->found_parents ) && in_array( $item->menu_item_parent, $this->found_parents ) ) ) {
  45.  
  46.             // Keep track of all selected parents
  47.             $this->found_parents[] = $item->ID;
  48.  
  49.             //check if the item_parent matches the current item_parent
  50.             if ( $item->menu_item_parent != $parent_item_id ) {
  51.  
  52.                 $output .= $indent . '<li id="menu-item-' . $item->ID . '"' . $value . $class_names . '>';
  53.  
  54.                 $attributes = !empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
  55.                 $attributes .=!empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
  56.                 $attributes .=!empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
  57.                 $attributes .=!empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
  58.  
  59.  
  60.                 // get user defined attributes for thumbnail images
  61.                 $attr_defaults = array( 'class' => 'nav_thumbnail ', 'alt' => esc_attr( $item->attr_title ), 'title' => esc_attr( $item->attr_title ) );
  62.                 $attr = isset( $args->thumbnail_attr ) ? $args->thumbnail_attr : '';
  63.                 $attr = wp_parse_args( $attr, $attr_defaults );
  64.                 $item_output = $args->before;
  65.                 $item_output .= '<a' . $attributes . '>';
  66.                 if ( $depth == 1 ) {
  67.                     // thumbnail image output
  68.                     $item_output .= ( isset( $args->thumbnail_link ) && $args->thumbnail_link ) ? '<a' . $attributes . '>' : '';
  69.                     $item_output .= apply_filters( 'menu_item_thumbnail', ( isset( $args->thumbnail ) && $args->thumbnail ) ? get_the_post_thumbnail( $item->object_id, ( isset( $args->thumbnail_size ) ) ? $args->thumbnail_size : 'thumbnail', $attr ) : '', $item, $args, $depth );
  70.                     $item_output .= ( isset( $args->thumbnail_link ) && $args->thumbnail_link ) ? '</a>' : '';
  71.                 }
  72.                 // menu link output
  73.                 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  74.                 // menu description
  75.                 $item_output .= '<span class="menu-item-description">' . $item->description . '</span>';
  76.                 // close menu link anchor
  77.                 $item_output .= '</a>';
  78.                 $item_output .= $args->after;
  79.  
  80.                 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  81.             }
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Ends the element output, if needed.
  87.      *
  88.      * The $args parameter holds additional values that may be used with the child class methods.
  89.      *
  90.      * @since 2.1.0
  91.      * @abstract
  92.      *
  93.      * @param string $output Passed by reference. Used to append additional content.
  94.      * @param object $object The data object.
  95.      * @param int    $depth  Depth of the item.
  96.      * @param array  $args   An array of additional arguments.
  97.      */
  98.     function end_el( &$output, $item, $depth ) {
  99.         // Closes only the opened li
  100.         if ( is_array( $this->found_parents ) && in_array( $item->ID, $this->found_parents ) ) {
  101.             $output .= "</li>\n";
  102.         }
  103.     }
  104.  
  105.     /**
  106.      * Starts the list before the elements are added.
  107.      *
  108.      * The $args parameter holds additional values that may be used with the child
  109.      * class methods. This method is called at the start of the output list.
  110.      *
  111.      * @since 2.1.0
  112.      * @abstract
  113.      *
  114.      * @param string $output Passed by reference. Used to append additional content.
  115.      * @param int    $depth  Depth of the item.
  116.      * @param array  $args   An array of additional arguments.
  117.      */
  118.     function start_lvl( &$output, $depth = 0 ) {
  119.         $output .= "\n<ul class=\"sub-menu\">\n";
  120.     }
  121.  
  122.     /**
  123.      * Ends the list of after the elements are added.
  124.      *
  125.      * The $args parameter holds additional values that may be used with the child
  126.      * class methods. This method finishes the list at the end of output of the elements.
  127.      *
  128.      * @since 2.1.0
  129.      * @abstract
  130.      *
  131.      * @param string $output Passed by reference. Used to append additional content.
  132.      * @param int    $depth  Depth of the item.
  133.      * @param array  $args   An array of additional arguments.
  134.      */
  135.     function end_lvl( &$output, $depth ) {
  136.         $indent = str_repeat( "\t", $depth );
  137.         // If the sub-menu is empty, strip the opening tag, else closes it
  138.         if ( substr( $output, -22 ) == "<ul class=\"sub-menu\">\n" ) {
  139.             $output = substr( $output, 0, strlen( $output ) - 23 );
  140.         } else {
  141.             $output .= "$indent</ul>\n";
  142.         }
  143.     }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement