我正在努力完成Walker_Nav_Menu docs。我想要一个关于将一个元素的子元素放入某个id但是我不确定从哪里开始的助行器。
我做了一些研究,但找不到任何基于页面ID的内容。是否有任何关于如何使其发挥作用的链接或参考?
感谢您的信息
答案 0 :(得分:4)
<强>解决!强>
我看错了,我试图比较帖子ID而不是菜单ID
这是我的完整助行器,Stephen Harris wp.tutsplus tutorial
的修改版本如果您没有传递菜单项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 );
}
}
}