Déjà Bond
Déjà Bond

Reputation: 301

Wordpress: add custom ID to ul in wp_nav_menu

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

Upvotes: 2

Views: 19297

Answers (3)

Stephen M. Harris
Stephen M. Harris

Reputation: 7533

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

Upvotes: 2

freejosh
freejosh

Reputation: 11383

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'
));

Upvotes: 5

Chris Herbert
Chris Herbert

Reputation: 6305

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

Upvotes: 1

Related Questions