Wordpress: add custom ID to ul in wp_nav_menu

16,429

Solution 1

wp_nav_menu accepts the key menu_id in its options array. Set it to the ID you want, e.g:

wp_nav_menu(array(
    'menu_id' => 'menu'
));

Solution 2

You can explicitly set the id in the html by defining items_wrap, and make sure walker is not set to some custom function:

wp_nav_menu( array(
    'theme_location' => 'main-menu'
    'items_wrap'     => '<ul id="menu" class="%2$s">%3$s</ul>',
    'walker'         => '',
)); 

This is incomplete info; 1st attempt to use:

'fallback_cb'     => false,

If you menu doesn't appear, that means you have not created any menu and that means its taking the fallback function take care for that.

So go and create a menu first. :D

Solution 3

Giving the ul an id that's the same as the class of its container is asking for trouble, but this should work:

<?php

function showMenu(){
$args = array(
  'menu_id' => 'menu'
);

wp_nav_menu($args);

}

showMenu();
?>

The WordPress Codex has a page detailing all options for the wp_nav_menu() function: http://codex.wordpress.org/Function_Reference/wp_nav_menu

Share:
16,429
Déjà Bond
Author by

Déjà Bond

Not a lot to say :|

Updated on July 17, 2022

Comments

  • Déjà Bond
    Déjà Bond almost 2 years

    I am trying to alter the wp_nav_menu to output the html like the below example.

    <div class="menu">
    <ul id="menu">
    

    The original output

    <div class="menu">
    <ul>
    

    I cant do it with jQuery or javascript, Its have to be php code

  • Déjà Bond
    Déjà Bond over 11 years
    the output is same the original, like i'm never edit the code!!
  • Déjà Bond
    Déjà Bond over 11 years
    the output is same the original, like i'm never edit the code!!
  • Chris Herbert
    Chris Herbert over 11 years
    Show us the output, please.
  • freejosh
    freejosh over 11 years
    @JimmyTodd Can you post the PHP code you're using to output the menu?
  • Déjà Bond
    Déjà Bond over 11 years
    this is the code img560.imageshack.us/img560/9905/wpmenug.png and this is the output jsfiddle.net/v9rvA
  • freejosh
    freejosh over 11 years
    What theme are you using? Maybe it disregards the option
  • Chris Herbert
    Chris Herbert over 11 years
    Are you sure you're editing the right file? Are you changing the code on your local server but refreshing the remote server? It really should add that id.
  • freejosh
    freejosh over 11 years
    Are you using any other plugins that might affect the output of the menu?
  • Déjà Bond
    Déjà Bond over 11 years
    no the all work i do in localhost and i'm really sure every thing is alright, and this why it driving me crazy :(
  • Chris Herbert
    Chris Herbert over 11 years
    As a test, remove wp_nav_menu() from your code completely. If the menu is still there then something else is going on.
  • Déjà Bond
    Déjà Bond over 11 years
    done it before and done it now too to be sure, when i remove the wp_nav_menu() the whole nave disappear