How do I remove UL on wp_nav_menu?

The function wp_nav_menu takes an argument of fallback_cb which is the name of the function to run if the menu doesn’t exist.
so change you code to something like this:

function wp_nav_menu_no_ul()
{
    $options = array(
        'echo' => false,
        'container' => false,
        'theme_location' => 'primary',
        'fallback_cb'=> 'fall_back_menu'
    );

    $menu = wp_nav_menu($options);
    echo preg_replace(array(
        '#^<ul[^>]*>#',
        '#</ul>$#'
    ), '', $menu);

}

function fall_back_menu(){
    return;
}

you can even remove the container from the menu and do other stuff with some more arguments sent to the wp_nav_menu function

Hope this helps.

Leave a Comment