Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My site's menu uses the built-in menu under Appearance >> Menus. I want to make a very simple change: I want to wrap each of my menu's second-level ULs in a DIV.

What's the simplest way to accomplish this?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Use a custom walker. Extend the functions start_lvl() and end_lvl():

class WPSE39005_Div_Walker extends Walker_Nav_Menu
{
    /**
     * @see Walker::start_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    public function start_lvl( &$output, $depth )
    {
        $output .= '<div><ul class="sub-menu">';
    }

    /**
     * @see Walker::end_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    public function end_lvl( &$output, $depth )
    {
        $output .= '</ul></div>';
    }
}

Call it in your theme like that:

<?php
// If there is no menu assigned we show nothing.
has_nav_menu( 'top-menu' )
// Path to the file with the walker relative to theme’s root.
and locate_template( 'php/class.WPSE39005_Div_Walker.php', TRUE, TRUE )
and wp_nav_menu(
    array (
    'menu'       => 'top-menu'
,   'walker'     => new WPSE39005_Div_Walker
    )
);
?>
share|improve this answer
    
That did exactly what I wanted. Thank you! –  Jason Swett Jan 15 '12 at 18:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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