WordPress Walker_Nav_Menu select children elements in menu based on page id

I’m having a hard time working through the Walker_Nav_Menu docs. I want to have a walker that about puts the children of an element with certain id but i’m not sure exactly where to start.

I did some research but couldn’t find anything based off of a Page ID. Does any one have any links or references on how to get this to work?

Read More

Thanks for the info

Related posts

Leave a Reply

1 comment

  1. 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 );
             }
        }
    }