• I found the following code on this stackexchange from this page Display a portion/ branch of the menu tree using wp_nav_menu(). It almost does what I need, except I need the whole menu, the item was chosen from, instead of the item name. I also need only 1 menu to show at a time. I basically would like to get different menus on each page, without using categories, based off the menu item chosen at that point. I really think this might work but because of my lack of skills I’ve just not been able to rewrite it so that it works how I need.

    This is the code.

    <?php
    
    add_filter( 'wp_nav_menu_objects', 'submenu_limit', 10, 2 );
    
    function submenu_limit( $items, $args ) {
    
        if ( empty($args->submenu) )
            return $items;
    
        $parent_id = array_pop( wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' ) );
        $children  = submenu_get_children_ids( $parent_id, $items );
    
        foreach ( $items as $key => $item ) {
    
            if ( ! in_array( $item->ID, $children ) )
                unset($items[$key]);
        }
    
        return $items;
    }
    
    function submenu_get_children_ids( $id, $items ) {
    
        $ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' );
    
        foreach ( $ids as $id ) {
    
            $ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
        }
    
        return $ids;
    }
    ?>
    
    <?php
    
    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 ) ) //unset ($children_elements);
                    $this->display_element( $e, $children_elements,2,0, $args, $output );
                   //$this->display_element( $e, $children_elements,$max_depth,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;
        }
    }
    ?>
    
    <?php wp_nav_menu(
       array(
           //'theme_location'=>'business',
            'menu'=>'business',
           'walker'=>new Selective_Walker() )
       ); ?>
    
     <?php wp_nav_menu(
       array(
           'theme_location'=>'personal',
           'walker'=>new Selective_Walker() )
       ); ?>

    The link to where I have also posted for help is below, no one has answered there either and I am not sure if I posted it wrong or something, and I am not sure if I am breaking any forum rules. I only mention it because I want to come clean that I know I posted it in 2 places. Please help, please share with your colleagues if you don’t know.

  • The topic ‘Different menus without categories’ is closed to new replies.