Removing WordPress menu div tag and keeping ul [Code Snippet]

When you are working with WordPress menu then sometimes you may noticed if no menu is defined it is putting a div wrapper just above the url tag. Here is how you can remove that div and keep ul as it as.

wp_nav_menu(
    array(
        'theme_location'  => '', //Just keep this as empty
        'container'       => '', //Keep this one also as empty
        'menu_class'      => 'menu',
        'menu_id'         => 'primary-menu',
        'depth' => '2',
    )
);

Checking if a menu published in WordPress

$has_enabled_main_menu = false;
$menu_location = 'primary'; //Going to check if this menu location (primary) has a menu as defined
$menu_locations = get_nav_menu_locations();
$menu_object = ( isset( $menu_locations[ $menu_location ] ) ? wp_get_nav_menu_object( $menu_locations[ $menu_location ] ) : null );
if( $menu_object ) {
    $has_enabled_main_menu = true;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: