1

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!

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.