Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Custom Walker to extract current sub-menu
- *
- * @since xx
- * @uses Walker
- */
- class Custom_Walker_Nav_Sub_Menu extends Walker_Nav_Menu {
- var $found_parents = array();
- /**
- * Start the element output.
- *
- * The $args parameter holds additional values that may be used with the child
- * class methods. Includes the element output also.
- *
- * @since 2.1.0
- * @abstract
- *
- * @param string $output Passed by reference. Used to append additional content.
- * @param object $object The data object.
- * @param int $depth Depth of the item.
- * @param array $args An array of additional arguments.
- * @param int $current_object_id ID of the current item.
- */
- function start_el( &$output, $item, $depth, $args ) {
- global $wp_query;
- //this only works for second level sub navigations
- $parent_item_id = 0;
- $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
- $class_names = $value = '';
- $classes = empty( $item->classes ) ? array() : (array) $item->classes;
- $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
- $class_names = ' class="' . esc_attr( $class_names ) . '"';
- // Checks if the current element is in the current selection (it is menu sublevel)
- 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 ) ) ) {
- // Keep track of all selected parents
- $this->found_parents[] = $item->ID;
- //check if the item_parent matches the current item_parent
- if ( $item->menu_item_parent != $parent_item_id ) {
- $output .= $indent . '<li id="menu-item-' . $item->ID . '"' . $value . $class_names . '>';
- $attributes = !empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
- $attributes .=!empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
- $attributes .=!empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
- $attributes .=!empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
- // get user defined attributes for thumbnail images
- $attr_defaults = array( 'class' => 'nav_thumbnail ', 'alt' => esc_attr( $item->attr_title ), 'title' => esc_attr( $item->attr_title ) );
- $attr = isset( $args->thumbnail_attr ) ? $args->thumbnail_attr : '';
- $attr = wp_parse_args( $attr, $attr_defaults );
- $item_output = $args->before;
- $item_output .= '<a' . $attributes . '>';
- if ( $depth == 1 ) {
- // thumbnail image output
- $item_output .= ( isset( $args->thumbnail_link ) && $args->thumbnail_link ) ? '<a' . $attributes . '>' : '';
- $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 );
- $item_output .= ( isset( $args->thumbnail_link ) && $args->thumbnail_link ) ? '</a>' : '';
- }
- // menu link output
- $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
- // menu description
- $item_output .= '<span class="menu-item-description">' . $item->description . '</span>';
- // close menu link anchor
- $item_output .= '</a>';
- $item_output .= $args->after;
- $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
- }
- }
- }
- /**
- * Ends the element output, if needed.
- *
- * The $args parameter holds additional values that may be used with the child class methods.
- *
- * @since 2.1.0
- * @abstract
- *
- * @param string $output Passed by reference. Used to append additional content.
- * @param object $object The data object.
- * @param int $depth Depth of the item.
- * @param array $args An array of additional arguments.
- */
- function end_el( &$output, $item, $depth ) {
- // Closes only the opened li
- if ( is_array( $this->found_parents ) && in_array( $item->ID, $this->found_parents ) ) {
- $output .= "</li>\n";
- }
- }
- /**
- * Starts the list before the elements are added.
- *
- * The $args parameter holds additional values that may be used with the child
- * class methods. This method is called at the start of the output list.
- *
- * @since 2.1.0
- * @abstract
- *
- * @param string $output Passed by reference. Used to append additional content.
- * @param int $depth Depth of the item.
- * @param array $args An array of additional arguments.
- */
- function start_lvl( &$output, $depth = 0 ) {
- $output .= "\n<ul class=\"sub-menu\">\n";
- }
- /**
- * Ends the list of after the elements are added.
- *
- * The $args parameter holds additional values that may be used with the child
- * class methods. This method finishes the list at the end of output of the elements.
- *
- * @since 2.1.0
- * @abstract
- *
- * @param string $output Passed by reference. Used to append additional content.
- * @param int $depth Depth of the item.
- * @param array $args An array of additional arguments.
- */
- function end_lvl( &$output, $depth ) {
- $indent = str_repeat( "\t", $depth );
- // If the sub-menu is empty, strip the opening tag, else closes it
- if ( substr( $output, -22 ) == "<ul class=\"sub-menu\">\n" ) {
- $output = substr( $output, 0, strlen( $output ) - 23 );
- } else {
- $output .= "$indent</ul>\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement