我已经胡闹/搜索了几个小时,但仍然无法实现这一点,所以我终于屈服了,并寻求一些帮助。
我正在尝试编写一个自定义助行器,它只显示当前页面的子级,或者如果没有子级,则显示页面的同级。
例如,以以下菜单树为例:
让我们假设我在当前的第1.2.0页。在此页面上,我想显示它的子项(1.3.0、1.3.1、1.3.2)
但是,如果我在第1.2.2页,因为它没有任何子级,它应该显示它的当前级别同级,因此它应该显示我(1.2.0、1.2.1、1.2.2)。
我已经胡闹/搜索了几个小时,但仍然无法实现这一点,所以我终于屈服了,并寻求一些帮助。
我正在尝试编写一个自定义助行器,它只显示当前页面的子级,或者如果没有子级,则显示页面的同级。
例如,以以下菜单树为例:
让我们假设我在当前的第1.2.0页。在此页面上,我想显示它的子项(1.3.0、1.3.1、1.3.2)
但是,如果我在第1.2.2页,因为它没有任何子级,它应该显示它的当前级别同级,因此它应该显示我(1.2.0、1.2.1、1.2.2)。
这是我用来只显示当前菜单项的子菜单项的walker。或者,如果它没有自己的子级,则可以选择“兄弟”菜单项。
全班都有注释解释每个部分
<?php
class SH_Child_Only_Walker extends Walker_Nav_Menu {
private $ID;
private $depth;
private $classes = array();
private $child_count = 0;
private $have_current = false;
// Don\'t start the top level
function start_lvl(&$output, $depth=0, $args=array()) {
if( 0 == $depth || $this->depth != $depth )
return;
parent::start_lvl($output, $depth,$args);
}
// Don\'t end the top level
function end_lvl(&$output, $depth=0, $args=array()) {
if( 0 == $depth || $this->depth != $depth )
return;
parent::end_lvl($output, $depth,$args);
}
// Don\'t print top-level elements
function start_el(&$output, $item, $depth=0, $args=array()) {
$is_current = in_array(\'current-menu-item\', $this->classes);
if( 0 == $depth || ! $is_current )
return;
parent::start_el($output, $item, $depth, $args);
}
function end_el(&$output, $item, $depth=0, $args=array()) {
if( 0 == $depth )
return;
parent::end_el($output, $item, $depth, $args);
}
// Only follow down one branch
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
// Check if element is in the current tree to display
$current_element_markers = array( \'current-menu-item\', \'current-menu-parent\', \'current-menu-ancestor\' );
$this->classes = array_intersect( $current_element_markers, $element->classes );
// If element has a \'current\' class, it is an ancestor of the current element
$ancestor_of_current = !empty($this->classes);
// check if the element is the actual page element we are on.
$is_current = in_array(\'current-menu-item\', $this->classes);
// if it is the current element
if($is_current) {
// set the count / ID / and depth to use in the other functions.
$this->child_count = ( isset($children_elements[$element->ID]) ) ? count($children_elements[$element->ID]) : 0;
$this->ID = $element->ID;
$this->depth = $depth;
$this->have_current = true;
if($this->child_count > 0) {
// if there are children loop through them and display the kids.
foreach( $children_elements[$element->ID] as $child ) {
parent::display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
}
} else {
// no children so loop through kids of parent item.
foreach( $children_elements[$element->menu_item_parent] as $child ) {
parent::display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
}
}
}
// if depth is zero and not in current tree go to the next element
if ( 0 == $depth && !$ancestor_of_current)
return;
// if we aren\'t on the current element proceed as normal
if(! $this->have_current )
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}
像在wp\\U nav\\U菜单上附加任何其他自定义助行器一样附加它<?php
wp_nav_menu( array(
\'menu\' => \'primary-menu\'
,\'container\' => \'nav\'
,\'container_class\' => \'subpages\'
,\'depth\' => 0
,\'walker\' => new SH_Child_Only_Walker()
));
?>
我也有类似的经历。您可能需要考虑将页面逻辑移出walker。基本上,将当前页面层次结构编译为对象。然后使用wp\\u nav\\u菜单功能中的“exclude”参数。现在,排除的页面将取决于当前页面是否有子页面。如果没有孩子显示兄弟;如果是儿童(&F)&;那些孩子是线尾秀的兄弟和孩子;如果是儿童(&F)&;而孙辈的存在排斥兄弟,并显示出子女和孙辈。
好的,我正在尝试显示我的主菜单,因此我使用以下代码:<?php $defaults = array( \'theme_location\' => \'\', \'menu\' => \'\', \'container\' => false, \'container_id\' => \'\', \'menu_class\' => \'men