3

So today I started work on my first Wordpress theme and so far it's been a lot of fun. However, I have had difficulty in getting wp_nav_menu() to do what I want.

Here's what I want:

<nav id="topnav" class="menu">
   <ul>
      <li class="current_page_item"><a href="#" title="Home">Home</a></li>
      <li class="page_item page-item-2"><a href="#" title="About">About</a></li>
   </ul>
</nav>

Looking in the documentation, I would expect the following call to do what I want:

<?php wp_nav_menu(array( 'container' => 'nav', 'container_id' => 'topnav' )); ?>

But instead I get this:

<div class="menu">
   <ul>
      <li class="current_page_item"><a href="#" title="Home">Home</a></li>
      <li class="page_item page-item-2"><a href="#" title="About">About</a></li>
   </ul>
</div>

To me it looks like my custom parameters are being ignored as the output from wp_nav_menu() is exactly the same. Is there something I need to turn on somewhere to enable this or is there something else going on?

EDIT

Curiously, if I change menu_class it alters the div's class (I expected the div to be the container) but changing menu_id does nothing.

EDIT 2

Even going into nav-menu-template.php and changing the defaults for container, container_id, container_class, menu_id does nothing. Changing menu_class to yyy will change the div's class from menu to yyy.

2
  • vote some of the answers so others can see which is more fitting for you. add some comments so it's easier to help.
    – hakre
    Aug 27, 2010 at 21:24
  • I believe the menu_id [ok I was mistaken: got confused between menu_id and menu... ;menu_id' should work as you expect it; the rest applies to 'menu'] argument is to let you specify the id of the menu you'd like displayed. Usually this is left blank so that the default behavior takes place: look for what menu is tied to the menu location specified by the theme_location argument
    – FOR
    Aug 29, 2010 at 21:53

4 Answers 4

6

i had the save problem: it's because if you don't create the menu in the admin wordpress use a fallback method and creates the menu out of all active pages ... and if this happens than the options from wp_nav_menu are not used ...

so: just create the menu in the admin and u can change the wrapper-tag to "nav"

1
  • +1 if no menus are available then fallback is used and arguments are ignored.
    – Rarst
    Jan 29, 2011 at 14:05
1

First of all, you won't be able to get WordPress to output any kind of <nav> element using native functions. <nav> is an HTML5 tag, and WordPress is built to generate XHTML output ... i.e. no <nav> element.


Edit: Apparently the function does allow nav ... but keep in mind that the rest of the WordPress core is still built to output XHTML, not HTML5 ... take care to make sure your site validates properly.


Try removing that reference ... it might be causing the error that's ignoring your 'container_id' => 'topnav' instruction. If things work right without the reference to nav you should have this:

<div id="topnav" class="menu">
   <ul>
      <li class="current_page_item"><a href="#" title="Home">Home</a></li>
      <li class="page_item page-item-2"><a href="#" title="About">About</a></li>
   </ul>
</div>
4
  • 1
    Thanks for the answer but wp_nav_menu(array('container_id' => 'topnav' )); still won't work right. Looking at this post (not the Codex) justintadlock.com/archives/2010/06/01/… where it explains the parameters: "container: The element that wraps around the list. The default is div but can be changed to nav if you’ve moved on to HTML 5."
    – Tom Savage
    Aug 25, 2010 at 15:30
  • What plug-ins are you using on the site? All of the arguments for wp_nav_menu are passed through the filter wp_nav_menu_args ... could there be some code on your site (in the theme or a plug-in) that's overriding your settings?
    – EAMann
    Aug 25, 2010 at 15:39
  • This is a fresh install of Wordpress 3.0.1. No plugins or strange confuguration. The only thing I've changed is the theme.
    – Tom Savage
    Aug 25, 2010 at 15:48
  • Curiously, if I change menu_class it alters the div's class (I expected the div to be the container) but changing menu_id does nothing.
    – Tom Savage
    Aug 25, 2010 at 15:52
1

The Menu API is partially broken, you can not rely on the output. There are diverse tickets in trac regarding that, I only did a quick search, but maybe a solution for your problem is explained in one of them:

1
  • Ah ok. For the time being I'll just output the menu to a string and use good old str_replace to make it work.
    – Tom Savage
    Aug 25, 2010 at 18:33
1

not sure if anyone is still looking to solve this - but just got here trying to find the answer myself - thought I'd share my solution :)

Also wanted to use the <nav> instead of the <div> element and solved it by removing the <div>, as stated on this page, wordpress codex: http://codex.wordpress.org/Function_Reference/wp_nav_menu:

<?php
function my_wp_nav_menu_args( $args = '' )
{
    $args['container'] = false;
    return $args;
} // function

add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
?>

Added to my functions.php file, marked up the theme file with the <nav> tag & all worked fine for me ;) hope this helps :)

Your Answer

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

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