• Resolved BShurilla09

    (@bshurilla09)


    My question is: how? I only want to expand the submenus of the current menu item, and leave the other top level menus unexpanded.
    My navigation is like so:

    Page 1
    Page 1 child a
    Page 1 child b

    Page 2
    Page 2 child a
    Page 2 child b
    Page 2 child b child 1

    Now if I am on Page 1 I want the children of page two to be hidden like so:

    Page 1
    Page 1 child a
    Page 1 child b

    Page 2

    is it really simple and I’m just missing this due to the 12:30 am or something else? Thanks…

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter BShurilla09

    (@bshurilla09)

    ok, so I don’t really know anything about this, walker function, but I’ve added this and called it in the theme, and this is it but the only problem is that it is hiding all the main items as well, but due to my lack of understanding this I’m not sure about how to edit the walker to fix that.. here is the walker code-

    class Selective_Walker extends Walker_Nav_Menu
    {
        function walk( $elements, $max_depth) {
    
            $args = array_slice(func_get_args(), 2);
            $output = '';
    
            if ($max_depth < -1) //invalid parameter
                return $output;
    
            if (empty($elements)) //nothing to walk
                return $output;
    
            $id_field = $this->db_fields['id'];
            $parent_field = $this->db_fields['parent'];
    
            // flat display
            if ( -1 == $max_depth ) {
                $empty_array = array();
                foreach ( $elements as $e )
                    $this->display_element( $e, $empty_array, 1, 0, $args, $output );
                return $output;
            }
    
            /*
             * need to display in hierarchical order
             * separate elements into two buckets: top level and children elements
             * children_elements is two dimensional array, eg.
             * children_elements[10][] contains all sub-elements whose parent is 10.
             */
            $top_level_elements = array();
            $children_elements  = array();
            foreach ( $elements as $e) {
                if ( 0 == $e->$parent_field )
                    $top_level_elements[] = $e;
                else
                    $children_elements[ $e->$parent_field ][] = $e;
            }
    
            /*
             * when none of the elements is top level
             * assume the first one must be root of the sub elements
             */
            if ( empty($top_level_elements) ) {
    
                $first = array_slice( $elements, 0, 1 );
                $root = $first[0];
    
                $top_level_elements = array();
                $children_elements  = array();
                foreach ( $elements as $e) {
                    if ( $root->$parent_field == $e->$parent_field )
                        $top_level_elements[] = $e;
                    else
                        $children_elements[ $e->$parent_field ][] = $e;
                }
            }
    
            $current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' );  //added by continent7
            foreach ( $top_level_elements as $e ){  //changed by continent7
                // descend only on current tree
                $descend_test = array_intersect( $current_element_markers, $e->classes );
                if ( !empty( $descend_test ) )
                    $this->display_element( $e, $children_elements, 2, 0, $args, $output );
            }
    
            /*
             * if we are displaying all levels, and remaining children_elements is not empty,
             * then we got orphans, which should be displayed regardless
             */
             /* removed by continent7
            if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
                $empty_array = array();
                foreach ( $children_elements as $orphans )
                    foreach( $orphans as $op )
                        $this->display_element( $op, $empty_array, 1, 0, $args, $output );
             }
            */
             return $output;
        }
    }

    Thread Starter BShurilla09

    (@bshurilla09)

    ok, so in my header I now have this:

    <!-- begin #nav_main_wrap -->
    <div id="nav_main_wrap"><div id="nav_main">
     <?php wp_nav_menu( array('theme_location'=>'primary','depth' => 1) ); ?>
    <span class="clear">&nbsp;</span>
    </div></div>
    <!-- end #nav_main_wrap -->
    <!-- begin #nav_sub_wrap -->
    <div id="nav_sub_wrap"><div id="nav_sub">
    	 <?php wp_nav_menu( array('theme_location'=>'primary','walker' =>new SH_Child_Only_Walker(),'depth' => 0) ); ?>
    	<span class="clear">&nbsp;</span>
    </div></div>
    <!-- end #nav_sub_wrap -->

    Which is displaying the top level of the menu fine, the walker for the sub menu seems to still be keeping it completely hidden, here is the walker function code

    class SH_Child_Only_Walker extends Walker_Nav_Menu {  
    
        // 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 )
                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 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 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 );
        }
    }

    Thread Starter BShurilla09

    (@bshurilla09)

    Ok, I was able to get it working! just had to add a missing colon in the walker function, here is the working walker:

    class SH_Child_Only_Walker extends Walker_Nav_Menu {  
    
        // 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 )
                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 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 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 );
        }
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Expand Page submenu items for current page Only’ is closed to new replies.