我具有以下菜单结构:
- Page 1
-- Subpage 1 of Page 1
-- Subpage 2 of Page 1
-- Subpage 3 of Page 1
- Page 2
- Page 3
- Page 4
现在,我想使用wp_nav_menu函数生成一个菜单,该菜单默认显示父页面。子页面(子级)仅在您位于具有子页面和子页面本身的页面上时显示。
这意味着在第1页上的Im时,菜单应类似于上面的菜单。当您位于页面1的子页面1或页面1的子页面2或页面1的子页面3上时,菜单也必须看起来像这样。
但是当Im在第4页(或第2页或第3页)上时,菜单应如下所示:
- Page 1
- Page 2
- Page 3
- Page 4
我该怎么做?
答案 0 :(得分:0)
您应该编写自定义WalkerMenu类:
class Custom_Walker_Nav_Sub_Menu extends Walker_Nav_Menu {
public $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>\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";
}
}
}
wp_nav_menu(
array(
'theme_location' => 'sidebar-menu', //assign nav in admin
'menu' => '',
'container' => 'div',
'container_class' => 'menu-{menu slug}-container',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul>%3$s</ul>',
'depth' => 0,
'walker' => new Custom_Walker_Nav_Sub_Menu()
)
);