Wordpress Walker_Nav_菜单根据页面id在菜单中选择子元素
我在工作中度过了一段艰难的时光。我想有一个助行器,它可以放置具有特定id的元素的子元素,但我不确定从何处开始 我做了一些研究,但找不到任何基于页面ID的内容。有没有人有任何链接或参考资料来说明如何使其工作Wordpress Walker_Nav_菜单根据页面id在菜单中选择子元素,wordpress,menu,Wordpress,Menu,我在工作中度过了一段艰难的时光。我想有一个助行器,它可以放置具有特定id的元素的子元素,但我不确定从何处开始 我做了一些研究,但找不到任何基于页面ID的内容。有没有人有任何链接或参考资料来说明如何使其工作 感谢您提供的信息已解决 我看起来完全错了,我试图比较帖子ID而不是菜单ID 这是我的完整步行器,修改版的 如果不传递菜单项ID,它将求助于“当前帖子”树 <?php /* Walker Class for selecting only current nav children. *
感谢您提供的信息已解决强> 我看起来完全错了,我试图比较帖子ID而不是菜单ID 这是我的完整步行器,修改版的 如果不传递菜单项ID,它将求助于“当前帖子”树
<?php
/* Walker Class for selecting only current nav children.
* Modified version of Stephen Harris class that adds in support for selecting based on menu_item_id
*
* @param int $menu_item_id ID of the menu item you want to select off of (optional)
*
* @author Jake Chamberlain
* @link http://jchamb.com
* @author Stephen Harris
* @link http://wp.tutsplus.com/tutorials/creative-coding/understanding-the-walker-class/
*/
class Selective_Walker extends Walker_Nav_Menu
{
var $menu_item;
function __construct($menu_item_id = false) {
$this->menu_item = $menu_item_id;
}
// Don't start the top level
function start_lvl(&$output, $depth=0, $args=array()) {
if( 0 == $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 )
return;
parent::end_lvl($output, $depth,$args);
}
// Don't print top-level elements
function start_el(&$output, $item, $depth=0, $args=array()) {
if( 0 == $depth && !$this->menu_item )
return;
parent::start_el($output, $item, $depth, $args);
}
function end_el(&$output, $item, $depth=0, $args=array()) {
if( 0 == $depth && !$this->menu_item )
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 as a 'current element' class
$current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' );
$current_class = array_intersect( $current_element_markers, $element->classes );
if( !$this->menu_item)
{
// If element has a 'current' class, it is an ancestor of the current element
$ancestor_of_current = !empty($current_class);
// If this is a top-level link and not the current, or ancestor of the current menu item - stop here.
if ( 0 == $depth && !$ancestor_of_current)
return;
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
else
{
if ( $this->menu_item != $element->menu_item_parent )
return;
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}
}
已解决强>
我看起来完全错了,我试图比较帖子ID而不是菜单ID
这是我的完整步行器,修改版的
如果不传递菜单项ID,它将求助于“当前帖子”树
<?php
/* Walker Class for selecting only current nav children.
* Modified version of Stephen Harris class that adds in support for selecting based on menu_item_id
*
* @param int $menu_item_id ID of the menu item you want to select off of (optional)
*
* @author Jake Chamberlain
* @link http://jchamb.com
* @author Stephen Harris
* @link http://wp.tutsplus.com/tutorials/creative-coding/understanding-the-walker-class/
*/
class Selective_Walker extends Walker_Nav_Menu
{
var $menu_item;
function __construct($menu_item_id = false) {
$this->menu_item = $menu_item_id;
}
// Don't start the top level
function start_lvl(&$output, $depth=0, $args=array()) {
if( 0 == $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 )
return;
parent::end_lvl($output, $depth,$args);
}
// Don't print top-level elements
function start_el(&$output, $item, $depth=0, $args=array()) {
if( 0 == $depth && !$this->menu_item )
return;
parent::start_el($output, $item, $depth, $args);
}
function end_el(&$output, $item, $depth=0, $args=array()) {
if( 0 == $depth && !$this->menu_item )
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 as a 'current element' class
$current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' );
$current_class = array_intersect( $current_element_markers, $element->classes );
if( !$this->menu_item)
{
// If element has a 'current' class, it is an ancestor of the current element
$ancestor_of_current = !empty($current_class);
// If this is a top-level link and not the current, or ancestor of the current menu item - stop here.
if ( 0 == $depth && !$ancestor_of_current)
return;
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
else
{
if ( $this->menu_item != $element->menu_item_parent )
return;
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}
}
你是摇滚明星!!感谢您发布您的解决方案!真棒@Jake Chamberlain,正是我想要的!你是个摇滚明星!!感谢您发布您的解决方案!真棒@Jake Chamberlain,正是我想要的!