How do I remove UL on wp_nav_menu?

Written by  on September 6, 2016 

I search on this site and found many answers ^(http://www.netaksharee.com/goto/http://wordpress.stackexchange.com/search?q=wp_nav_menu) for this question. Most of them is not working on my theme.

Here is a one solution I found ^(http://www.netaksharee.com/goto/http://techblog.willshouse.com/2011/01/15/how-to-remove-the-container-with-the-wp_nav_menu-function/) and it’s working according to my need.

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

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

}

This code will remove ul at beginning and the end of wp_nav_menu(). So in my theme I just write

<ul class="primary-nav">
<?php  wp_nav_menu_no_ul(); ?>
</ul>

But the problem is coming again when I do not add or activate any menu via admin. http://domain.com/wp-admin/nav-menus.php

Question :

How do I remove the <div><ul>**</ul></div> whether the menu is active or not. Let me know


Finally I got it worked 🙂 functions.php

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

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

}

function default_page_menu() {
   wp_list_pages('title_li=');
}

header.php

<ul class="primary-nav">
<?php  wp_nav_menu_no_ul(); ?>
</ul>

Category : Wordpress

Tags :