/ Published in: PHP
No modifications or testing carried out yet.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Custom Walker to extract current sub-menu */ class Custom_Walker_Nav_Sub_Menu extends Walker_Nav_Menu { function start_el(&$output, $item, $depth, $args) { global $wp_query; //this only works for second level sub navigations $parent_item_id = 0; $class_names = $value = ''; $class_names = ' class="' . esc_attr( $class_names ) . '"'; $output.= ' <li>'; } // Checks if the current element is in the current selection // 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 .'>'; $item_output = $args->before; $item_output .= '<a'. $attributes .'>'; $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; $item_output .= '</a>'; $item_output .= $args->after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } } } function end_el(&$output, $item, $depth) { // Closes only the opened li $output .= "</li>\n"; } } function end_lvl(&$output, $depth) { // If the sub-menu is empty, strip the opening tag, else closes it } else { $output .= "$indent</ul>\n"; } } }