23

I search on this site and found many answers for this question. Most of them is not working on my theme.

Here is a one solution I found 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>
2
  • Why do you want to remove the UL when you add it again anyway? You can use the menu_class and menu_id parameters to set a class and/or id attribute on the UL.
    – user2370
    Commented Jan 28, 2011 at 13:52
  • I want use superfish. menu_class and menu_id only will working if menu is activated. If the menu doesn't exists menu_class and menu_id is not as <ul> but as <div>. thats why too much question about wp_nav_menu() you may test it :)
    – haha
    Commented Jan 28, 2011 at 14:10

7 Answers 7

11

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.

6
  • @בניית אתרים - Thanks for your explanation. But I'm getting blur here. According to the note on nav-menu-template.php fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'. Set to false for no fallback What should I replace to make wp_nav_menu_no_ul() working if I set false to the fallback_cb ? Because it return blank for now.
    – haha
    Commented Jan 28, 2011 at 11:49
  • Now understood :) Thanks a lot בניית אתרים. BTW, how do I pronounce your nickname? Mine ha ha
    – haha
    Commented Jan 28, 2011 at 12:08
  • LOL, glad you go it.
    – Bainternet
    Commented Jan 28, 2011 at 12:17
  • I've wondered the same thing, it's Hebrew i assume, what would be an accurate translation, out of curiousity.. :)
    – t31os
    Commented Jan 28, 2011 at 15:55
  • :) yes it Hebrew and its a nick i picked up a while a go that if properly translated means something like "web design".
    – Bainternet
    Commented Jan 28, 2011 at 16:17
79

Actually, WordPress supports this by default:

wp_nav_menu(array(
    'items_wrap' => '%3$s'
));

The default for items_wrap is <ul id=\"%1$s\" class=\"%2$s\">%3$s</ul>.

6
  • 3
    This is the correct answer. The other answers of doing a programmatic find & replace to remove <ul> after it has already been added are just backwards
    – mikemike
    Commented Mar 20, 2015 at 10:51
  • 1
    'container' => false if you want to get rid of the wrapping <div> as well. Commented Aug 10, 2017 at 10:11
  • It is true configurable in WordPress by default. Thanks! Commented Apr 2, 2018 at 11:44
  • best answer, no need for hack
    – Eman
    Commented Feb 2, 2019 at 2:58
  • 1
    this is not working for me in wp 5.2
    – rafaelphp
    Commented Jun 30, 2019 at 16:48
8

The below code should simple do it.

<?php 

  $my_menu = array( 
  'menu' => 'main-menu',
  'container' => '',
  'items_wrap' => '%3$s' 
  );

  wp_nav_menu( $my_menu );

?>

Reference this link for the wp_nav_menu function http://codex.wordpress.org/Function_Reference/wp_nav_menu

5

If you want to print only <a> tags, you may go this way:

$primaryMenu = array(
    'theme_location'  => 'primary',
    'menu'            => '',
    'container'       => '',
    'container_class' => false,
    'container_id'    => '',
    'menu_class'      => 'menu',
    'menu_id'         => 'primary-menu',
    'echo'            => false,
    'fallback_cb'     => 'wp_page_menu',
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'depth'           => 0,
    'walker'          => ''
);
echo strip_tags( wp_nav_menu( $primaryMenu ), '<a>' );
1
  • this work perfect for me, setting only ` 'items_wrap' => '%3$s' ` does not has effect in wp 5.2
    – rafaelphp
    Commented Jun 30, 2019 at 16:50
3

For me what worked was this:

<?php wp_nav_menu( array(
       'container' => '',
       'items_wrap' => '%3$s' 
) ); ?>

Hope it helps.

1

I know that this answer is not completely for this question but there are so many people who come to know how to remove ul and li tag in WordPress and add another tag in WordPress.

Like before applying my code WordPress gives these types of output in menu

<ul class="*****"><li>abc</li></ul>

But someone wants to change ul into div and li into a tag then you should use below code

            <?php

                $menuParameters = array(
                    'menu' => 'primary_menu',
                    'link_before'     => '<span>',
                    'link_after'      => '</span>',
                    'before'        => '<div class="tp-primary-header mui-top-home">',
                    'after'     => '</div>',
                    'container'       => false,
                    'echo'            => false,
                    'depth'           => 0,
                );

                echo strip_tags(wp_nav_menu( $menuParameters ), '<a><span><div>' );
                ?>

This gives output in the following format

<div class="tp-primary-header mui-top-home"><a href="#"><span>ABC</span></a></div>
<div class="tp-primary-header mui-top-home"><a href="#"><span>def</span></a></div>
<div class="tp-primary-header mui-top-home"><a href="#"><span>XYZ</span></a></div>
0
<?php
$params = array(
    'theme_location' => 'menu_location',
    'container' => false,
    'echo' => false,
    'items_wrap' => '%3$s',
);

echo strip_tags(wp_nav_menu($params), '<a>');
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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