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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745647737a4638073.html

相关推荐

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

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

    22天前
    90

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信