1

我很难通过Walker_Nav_Menu 文档工作。我想要一个助行器,它可以放置具有特定 id 元素的子元素,但我不确定从哪里开始。

我做了一些研究,但找不到任何基于页面 ID 的东西。有没有人有任何关于如何让它工作的链接或参考?

谢谢(你的)信息

4

1 回答 1

4

Solved!

I was looking a it all wrong, i was trying to compare post ID instead of menu ID

here's my full walker, modified version of Stephen Harris wp.tutsplus tutorial

if you don't pass an menu item ID it resorts to the current posts tree.

<?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 );
         }
    }
}
于 2012-08-15T19:13:46.450 回答