WordPress.org

Ready to get started?Download WordPress

Forums

Nav Menu, Remove Container (34 posts)

  1. LSCare
    Member
    Posted 2 years ago #

    Hello All,

    I am having a massive issue with a menu...

    I want to remove the div container from the nav menu created by:

    wp_nav_menu( array( 'theme_location' => 'primary', 'container' => '' ) );

    (based in header.php)

    and the menu is registered by:

    register_nav_menus( array(
        'primary' => __( 'Primary Menu', 'makin' ),
    ) );

    (based in functions.php)

    with the theme folder named makin and the theme named makin.

    Anyone got any ideas?
    __________________
    Laurence S Care

  2. chreo at wp
    Member
    Posted 2 years ago #

    You will need to get rid of the div-tags themselves in header.php
    <div id="container"> and </div>, but then you would have no destination for the nav if you cant replace 'container' with something else.
    Is there any markup you can use to
    replace 'container'? Any nested named tag, or is the nav the direct child of #container?

    This would be easier if you copied the relevant source-code from your page, so we could see the structure.

  3. LSCare
    Member
    Posted 2 years ago #

    I wish it were that simple.

    My problem is that the wp_nav_menu() outputs the following:

    <div class='menu'>
    <ul>
    <li>xxx</li>
    <li>xxx</li>
    <li>xxx</li>
    </ul>
    </div>

    so you need to use the function's arguments 'container' => '' to remove that output.

    Unfortunately I cannot get the function to not overlook the argument above. Even though so far as I can tell I have done all that is required by: the function reference (section quoted below)

    In order to remove navigation container, theme location specified in functions.php and used among arguments in function wp_nav_menu ( eg. 'theme_location' => 'primary-menu' ) must have a menu assigned to it in administration! Othervise argument 'container' => 'false' is ignored.

    Laurence S Care

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Uh? That markup is generated by the wp-nav_menu function.

    @LSCare
    : See http://codex.wordpress.org/Function_Reference/wp_nav_menu

  5. chreo at wp
    Member
    Posted 2 years ago #

    So, the <ul> is your actual nav-menu, the <li> tags are the menu-items, sub-menus would be nested in the </li> tags, right?

  6. LSCare
    Member
    Posted 2 years ago #

    @esmi: I have looked and am sure that the two bits of code in my original post satisfy the requirements on that page.

    @chreo: the menu items are listed by the li which is surrounded by the ul. That is all surrounded by the div.

  7. LSCare
    Member
    Posted 2 years ago #

    Here is all of the relevant code:

    <nav>
    <?
        wp_nav_menu(
            array( 'theme_location' => 'primary', 'container' => false )
        );
    ?>
    </nav>

    in header.php

    register_nav_menus( array(
    	'primary' => __( 'Primary Menu', 'makin' ),
    ) );

    in functions.php

    Based on what the function reference says... That is all I beleive I need but it still refuses to work.

    NB. I am aware I use shorthand <? but I have confirmed it is active on the server so it is not the lack of <?php at the beginning of the php code.

  8. chreo at wp
    Member
    Posted 2 years ago #

    Good for you!

  9. chreo at wp
    Member
    Posted 2 years ago #

    Have you tried this?
    <?php wp_nav_menu( array( 'container' => '' ) ); ?>
    It's from the reference, and I think it should work, since you can use it IF you have an assigned menu, which you do.

  10. LSCare
    Member
    Posted 2 years ago #

    I did and it made no difference I'm afraid.

  11. chreo at wp
    Member
    Posted 2 years ago #

    @esmi What would happen if he just deleted

    'container'       => 'div',
      'container_class' => 'menu-{menu slug}-container',
      'container_id'    => ,

    from the functions.php?

  12. LSCare
    Member
    Posted 2 years ago #

    My functions.php doc has very literally only got:

    <?
    
    register_nav_menus( array(
    	'primary' => __( 'Primary Menu', 'makin' ),
    ) );
    
    ?>

    in it. There is no other code.

    edit: just off to lunch so will pick up when i return... thanks for the help so far

  13. chreo at wp
    Member
    Posted 2 years ago #

    Found where the default container comes from: /wp-includes/nav-menu-template.php
    I have linked to the relevant portion.
    Try commenting out

    <?php
    $defaults = array( 'menu' => '',
    /**'container' => 'div', 'container_class' => '', 'container_id' => '', */
    'menu_class' => 'menu',
    ?>

    It's a brute force approach, and probably much frowned upon, but if you can find no other solution . . . and it still might not work unless you change things elsewhere afterwards.

    A thought: have you made sure you are not showing cached pages when you check? I use Chrome, and I HAVE TO empty the cache to be sure I am seeing my latest changes.

  14. LSCare
    Member
    Posted 2 years ago #

    I can't do that though because if I want to use the same function with different criteria later then I wouldn't be able to.

    I must be either missing something or doing something wrong but I need to be able to manipulate the container via the function arguments.

    All of that is ignoring my want to not manipulate the API functions for the obvious reason that one change can be quite destructive if a mistake is made.

  15. chreo at wp
    Member
    Posted 2 years ago #

    I just had another thought: if 'makin' is the default menu for the theme, you don't have an assigned menu!
    In that case, what you need to do is go to menus in the dashboard and create a new one that duplicates what 'makin-default' does and assign that to Primary.
    If you can't fix it with <?php wp_nav_menu( array( 'container' => '' ) ); ?> after that, something is seriously wrong.

  16. esmi
    Forum Moderator
    Posted 2 years ago #

    I've just tested wp_nav_menu( array( 'theme_location' => 'primary', 'container' => false ) ); and it's working fine for me. No enclosing container.

  17. LSCare
    Member
    Posted 2 years ago #

    @esmi Which theme are you doing that in and could you show me the registering function from the functions.php file please.

    and makin by the way is the name of the folder and theme, the menu is named using primary.

  18. chreo at wp
    Member
    Posted 2 years ago #

    I just checked my /themes/functions.php and I have

    register_nav_menu( 'primary', __( 'Primary Menu', 'twentyeleven' ) );

    so if you have

    register_nav_menu( 'primary', __( 'Primary Menu', 'makin' ) );

    'makin' IS your default, you do not have an assigned menu:

    . . . . must have a menu assigned to it in administration!

    administration meaning dashboard.

  19. esmi
    Forum Moderator
    Posted 2 years ago #

    Which theme are you doing that in

    One of my own themes - literally.

    could you show me the registering function

    register_nav_menus( array(
    		'primary' => __( 'Primary Menu', 'collage' ),
    ) );
  20. esmi
    Forum Moderator
    Posted 2 years ago #

    'makin' IS your default

    Uh? No - that's the theme's textdomain name. It has nothing to do with it.

  21. LSCare
    Member
    Posted 2 years ago #

    register_nav_menus( array(
    'primary' => __( 'Primary Menu', 'collage' ),
    ) );

    So then I assume collage is the name of the themes folder or the name of the theme?

    If it is... do you have any other functions setting up that menu in the functions.php file?

  22. chreo at wp
    Member
    Posted 2 years ago #

    OK, I just checked, and for me, when I use wp_nav_menu( array( 'theme_location' => 'primary', 'container' => false ) ); on my page, it works just fine, until I go to dashboard and remove the custom-menu I assigned to Primary.
    Then, the container-div comes back.
    In dashboard the select-area is blank, when the default is active.
    What does it look like for you?

  23. esmi
    Forum Moderator
    Posted 2 years ago #

    I assume collage is the name of the themes folder or the name of the theme?

    It's the name of theme. Or rather it's the theme's textdomain name and using it allows the theme to be translated easily. See /I18n_for_WordPress_Developers.

    do you have any other functions setting up that menu in the functions.php file?

    Nope.

  24. esmi
    Forum Moderator
    Posted 2 years ago #

    Oh - I have:

    // Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
    function collage_page_menu_args( $args ) {
    	$args['show_home'] = true;
    	return $args;
    }
    add_filter( 'wp_page_menu_args', 'collage_page_menu_args' );

    but that won't affect the enclosing markup for a custom menu. It only works when the default nav is being used (wp_page_menu) - in which case, a Home item is shown by default.

  25. chreo at wp
    Member
    Posted 2 years ago #

    I looked at an unmodified TwentyTen theme, and before you create a "menu" in "menus", you can't see the select-box I was talking about.
    If you can't see the select-box, you are using the default menu, not an assigned one.
    It could be what is causing your problem, so I really think you should check.

  26. LSCare
    Member
    Posted 2 years ago #

    I am using the default menu but i believe the default menu is called Primary Menu so when registering a menu if i register Primary Menu i am registering the default page menu.

    @esmi This translation thing may be getting somewhere... If you do not know this then I will read a bit deeper. but to make that textdomain, is that done via any other function or is it just taken from the name of the theme.

  27. chreo at wp
    Member
    Posted 2 years ago #

    Why don't you just take my suggestion and create a menu, you don't even have to add links, just name it and assign it to Primary and see what happens.

  28. LSCare
    Member
    Posted 2 years ago #

    Because a duplicate menu would still need updating everytime you wish to add a page.

    That's not something I would want to ask a third party to do.

    If I were making the site for myself then I would but I'd much rather not for someone else.

  29. esmi
    Forum Moderator
    Posted 2 years ago #

    to make that textdomain, is that done via any other function or is it just taken from the name of the theme

    In order for people to be able to use their own language files, you have add something to functions.php. I typically use something like:

    load_theme_textdomain( 'collage', get_template_directory() . '/languages' );
    $locale = get_locale();
    $locale_file = get_template_directory() . "/languages/$locale.php";
    if ( is_readable( $locale_file ) ) require_once( $locale_file );

    but that won't affect the generated markup in any way. Listen to chreo. He(?) has a good point. If you have not set up a custom menu & assigned it to your Primary location yet, your theme/site will be using the default fallback - which is wp_page_menu(). That has its own, independent, parameters and generated output. wp_nav_menu's parameters only kick in when your custom menu is being displayed.

    The bad news is that there isn't a parameter in wp_page_menu to remove its enclosing <div></div>. The good news is that you don't have to fallback to wp_page_menu. You could use (say) wp_list_pages() or something else entirely. You can specify exactly what function what you want to use via the fallback_cb parameter in wp_nav_menu().

  30. chreo at wp
    Member
    Posted 2 years ago #

    @esmi It's she, but thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic