• Hello!

    I’ve tried to delete the div and ul tags from the function wp_nav_menu() by defining the parameter container as none but the div anf ul tags still showing.

    This is the whole code of my wp_nav_menu() function and parameters:

    wp_nav_menu(
    array(
    'menu_class' => 'Navigation',
    'container' => 'none',
    'container_class' => 'menu-header',
    'theme_location' => 'primary'
    ));

    How can I resolve that issue?

    Thanks!

Viewing 12 replies - 16 through 27 (of 27 total)
  • @scopy, I was having similar issue. WP’s default container and classes were being added to my custom menu.

    Everything works now after assigning my menu to the defined location, which I am specifying in my call to wp_nav_menu(). With no menu assigned to the location I was searching for, the default fallback_cb was adding the container.

    @emil, thanks for the code to remove the <ul> tags.

    @zhannes glad to hear that you found the code useful πŸ™‚

    Thread Starter Yonatan Ganot

    (@xxxyonixxx)

    @zhannes

    I’ve tried this but it isn’t works for me.

    Can you post your code so I will able to see how it’s looks like?

    Actually the container is not really the <ul>, it is the <div> that wraps the <ul>. So if you set the container to false or ” you are just getting rid of the <div> but not the <ul>.

    Emil is on the right track, but I had trouble getting the add_filter( 'wp_nav_menu', 'remove_ul' ) to actually work.

    So, I think this code will actually work for you:

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

    I put a more detailed explanation on my blog: How to Remove ul from wp_nav_menu

    @emil and @willshouse Thank you. Tested both of your solutions to remove <ul> and </ul> from wp_nav_menu and both worked. Thanks to @Scolpy for starting this topic πŸ™‚

    I wanted to update a theme currently used in a live installation to implement wp_nav_menu but fall back to wp_list_pages and use existing styling I had in my theme’s style.css for elements within <div id="navbar"> and <ul id="nav">

    To summarize one solution where I can easily spot what needs to be changed later on:

    In functions.php

    // register wp_nav_menu
    add_action( 'init', 'register_my_menus' );
    function register_my_menus() {
    	register_nav_menus( array(
    	'primary-menu' => __( 'Primary Menu', 'mytheme' )
    	)
    	);
    }
    
    // remove ul wp_nav_menu
    function remove_ul ( $menu ){
        return preg_replace( array( '#^<ul[^>]*>#', '#</ul>$#' ), '', $menu );
    }
    add_filter( 'wp_nav_menu', 'remove_ul' );

    In header.php

    <div id="navbar">
    	<ul id="nav">
    	<?php if ( has_nav_menu( 'primary-menu', 'mytheme' ) ) { ?>
    		<?php wp_nav_menu( array( 'container' => false, 'theme_location' => 'primary-menu', 'fallback_cb' => 'display_home' ) ); ?>
    	<?php } else { ?>
     		<li><a href="<?php echo get_option('home'); ?>">Home</a></li>
    		<?php wp_list_pages('title_li=&depth=4&sort_column=menu_order'); ?>
    	<?php	} ?>
    	</ul>
    </div>

    Found in trac – Add ability to remove ul tag from wp_nav_menu result

    Another way using second – http://wordpress.stackexchange.com/questions/7968/how-do-i-remove-ul-on-wp-nav-menu

    Cheers.

    All good stuff..
    However if like myself you would like to strip both UL and LI tags and only affect one of your menus in this case my Secondry Menu located n the footer you want something like this:

    <?php $foot_nav = wp_nav_menu( array( 'container' => '', 'echo' => '0', 'theme_location' => 'secondary-menu' ) );
    	 $foot_nav2 = preg_replace( array( '#^<ul[^>]*>#', '#</ul>$#' ), '', $foot_nav );
    	 $foot_nav2 = preg_replace( array( '#<li[^>]*>#', '#</li>$#' ), '', $foot_nav2 );
    	 echo $foot_nav2;
    	  ?>

    Just paste it where you want it to appear in your theme.
    N.B:
    You will need to amend the 'theme_location' => 'secondary-menu'
    to match the menu you are using.

    Enjoy!

    grubbs

    (@grubbs)

    I had problems when trying to remove the div container.
    I checked my menu config in the admin Appearence -> Menu and see it was not set correctly… so please forget the code 5 minutes and double-check this. πŸ˜‰

    Cheers!

    Zambrano Sergio

    (@sergiozambrano)

    [SOLVED]

    CONTAINER => FALSE FAILS if you are specifying a location is not found,
    e.g. when you copy the code from somewhere else and you haven’t specified a menu or location yet.

    It was driving me CRAZY!!

    You can see it working fine in my website πŸ™‚ SocialBlogsiteWebDesign.com

    For any one with this issue use the extra param:

    'items_wrap' => '%3$s'

    found here:
    http://wordpress.stackexchange.com/questions/2574/remove-wrapping-div-and-ul-from-output-of-wp-nav-menu/13347#13347

    This is what I used to produce a HORIZONTAL wp_nav_menu. (no drop downs though – see my earlier link for drop downs)

    Make sure this is in your functions.php

    if (function_exists('register_nav_menus')) {
       	register_nav_menus(
    		array(
    			'main_nav' => 'Main Navigation Menu'
    			)
    		);
    	}

    place the wp_nav_menu where you want to in your code
    <div id="information"><?php wp_nav_menu(array('menu' => 'Main Nav Menu', 'container' => false, 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>')); ?></div>

    Add CSS
    http://pastebin.com/SdpvYABV

    One point to add: My menu in the wp admin area was called Main Nav Menu.
    NOTE: <?php wp_nav_menu(array('menu' => 'Main Nav Menu',

    At least I found out that I’m not the only one having trouble with the wp_nav_menu…

    @sergiozambrano or anyone else here;
    what do you mean by specifying a menu or a location?
    is that really important?

    To my problem; all I want is giving the wrapping ul an id called “nav”.

    <?php wp_nav_menu(array('container' => 'ul', 'menu_id'=>'nav')); ?>

    I read this code-fragment in a tutorial-book about wordpress themes, so it should work. but it doesn’t. My ul just doesn’t get this id…

    Can somebody help me out with this? I read all the stuff here, but can’t get it working…

    And about that items_wrap. Where do I have to specify the variables which get inserted at
    %1$s,%2$s,%3$s?

Viewing 12 replies - 16 through 27 (of 27 total)
  • The topic ‘Remove the div and ul tags from wp_nav_menu() function’ is closed to new replies.