最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Using a shortcode to create a dropdown menu from wp_nav_menu list items

matteradmin7PV0评论

I'm using the following function that I found online to render each page's individual nav items to the page:

function print_menu_shortcode($atts, $content = null) {
    extract(
        shortcode_atts(
            array(
                'name' => null, 
                'class' => null
            ), 
            $atts
        )
    );

    return wp_nav_menu(
        array(
            'menu' => $name, 
            'menu_class' => $class, 
            'echo' => false
        )
    );
}

I would like to display these nav links in a dropdown menu with a button labeled Menu instead of just having the links render as an ul, but I don't know how to access the individual items within wp_nav_menu.

Any help would be the best, thanks!

I'm using the following function that I found online to render each page's individual nav items to the page:

function print_menu_shortcode($atts, $content = null) {
    extract(
        shortcode_atts(
            array(
                'name' => null, 
                'class' => null
            ), 
            $atts
        )
    );

    return wp_nav_menu(
        array(
            'menu' => $name, 
            'menu_class' => $class, 
            'echo' => false
        )
    );
}

I would like to display these nav links in a dropdown menu with a button labeled Menu instead of just having the links render as an ul, but I don't know how to access the individual items within wp_nav_menu.

Any help would be the best, thanks!

Share Improve this question edited Mar 29, 2019 at 4:33 Sven 3,6841 gold badge35 silver badges48 bronze badges asked Mar 28, 2019 at 23:29 Brianne DuffyBrianne Duffy 131 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can use wp_get_nav_menu_items() which retrieves all menu items of a navigation menu. Your shortcode callback should be something like this .

  function print_menu_shortcode($atts, $content = null) {
       extract( shortcode_atts(
            array(
                'name' => null, 
                'class' => null
            ), 
            $atts
        ));

        // Assuming $name contains slug or  name of menue
        $menu_items = wp_get_nav_menu_items($name); 

        // Sample Output. Adjsut as per your exact requirements.

        // Sample Output variable.
        $menu_dropdown  = '';

        if ($menu_items){

            $menu_dropdown  .= '<select onChange="document.location.href=this.options[this.selectedIndex].value;">';

            foreach( $menu_items as $menu_item ) {

               $link = $menu_item->url;
               $title = $menu_item->title;
               $menu_dropdown  .= '<option value="' . $link .'">'. $title . '</option>' ;

            }

             $menu_dropdown  .= '</select>';

        } else {
           $menu_dropdown  = '<!-- no menu defined in location "'.$theme_location.'" -->';
        }

           return $menu_dropdown ;
}

More Examples on use of this function.

Post a comment

comment list (0)

  1. No comments so far