Created
November 13, 2018 19:37
-
-
Save petertwise/718eccceb495ec468d294f538c6c87f7 to your computer and use it in GitHub Desktop.
Examples of extending WordPress Walker_Nav_Menu for sibling and subpage menus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom Walker to extract current sister-sub-menu | |
*/ | |
class Custom_Walker_Nav_Sister_Menu extends Walker_Nav_Menu { | |
var $found_parents = array(); | |
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 ) . '"'; | |
if ( ($item->menu_item_parent==0) && (strpos($class_names, 'current-menu-parent')) ) { | |
$output.= ' | |
<li>'; | |
} | |
// Checks if the current element is in the current selection | |
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 ) .'"' : ''; | |
$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 | |
if ( is_array($this->found_parents) && in_array( $item->ID, $this->found_parents ) ) { | |
$output .= "</li><!--". $item->menu_item_parent."-->"; | |
} | |
} | |
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 .= "</ul>"; | |
} | |
$emptysubmenu = '<ul class="sub-menu"> | |
</ul>'; | |
$output = str_replace($emptysubmenu, '', $output); | |
} | |
} | |
/* END SISTER CUSTOM MENU WALKER */ | |
/** | |
* Custom Walker to extract current sub-menu | |
*/ | |
class Custom_Walker_Nav_Sub_Menu extends Walker_Nav_Menu { | |
var $found_parents = array(); | |
function start_el(&$output, $item, $depth, $args) { | |
global $wp_query; | |
$id = get_the_ID(); | |
//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 ) . '"'; | |
if ( ($item->menu_item_parent==0) && (strpos($class_names, 'current-menu-parent')) ) { | |
$output.= ' | |
<li>'; | |
} | |
// Checks if the current element is in the current selection | |
if (strpos($class_names, 'current-menu-item') | |
|| (is_array($this->found_parents) && in_array( $item->menu_item_parent, $this->found_parents )) ) { | |
// if ($item->menu_item_parent==$id) { | |
// 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 && !strpos($class_names, 'current-menu-item') ){ | |
$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 ) .'"' : ''; | |
if (!strpos($class_names, 'current-menu-item')) { | |
$attributes .= ' id="subpage_link_' . $item->object_id .'"'; | |
$attributes .= ' class="show-subpage-ajax"'; | |
} | |
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : ''; | |
$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 | |
if ( is_array($this->found_parents) && in_array( $item->ID, $this->found_parents ) ) { | |
$output .= "</li>\n"; | |
} | |
} | |
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"; | |
} | |
$emptysubmenu = '<ul class="sub-menu"> | |
</ul>'; | |
$output = str_replace($emptysubmenu, '', $output); | |
} | |
} | |
/* END SUBPAGE CUSTOM MENU WALKER */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment