I'm using wp_nav_menu and am trying to create custom output for the sub-level drop downs. I came across the "items_wrap" argument but there's really not much information as to what it is, how it works, and what kind of things can be done with it.

What exactly is "%1$s" and "%2$s"? (Can anyone explain it in layman's terms?)

up vote 52 down vote accepted

The parameter 'items_wrap' for wp_nav_menu() defaults to:

'<ul id="%1$s" class="%2$s">%3$s</ul>' 

This a a template that is parsed with sprintf():

$nav_menu .= sprintf( 
    $args->items_wrap
,   esc_attr( $wrap_id )    // %1$s
,   esc_attr( $wrap_class ) // %2$s
,   $items                  // %3$s
);

The numbered placeholders – %1$s, %2$s, %3$s – refer to the arguments after the first argument in sprintf(). The percent sign marks a placeholder, the number the position and the type s means it should be treated as a string.
Do not change the type unless you really know what you do. :)

  • $wrap_id is the parameter 'menu_id' if you have it set, else it is 'menu-' . $menu->slug.
  • $wrap_class is the parameter 'menu_class' if you have it set, else it is empty.
  • $items is a string of the inner content of the menu.

Let’s say you don’t need a class. Just omit the second string:

wp_nav_menu( array( 'items_wrap' => '<ul id="%1$s">%3$s</ul>' ) );

If you don’t need the class and the id, and you want another container (because you used a custom walker):

wp_nav_menu( array( 'items_wrap' => '<div>%3$s</div>' ) );

The main point is: You have to use the numbers for the replacements given in wp_nav_menu(). %3$s is always the list of items.

  • Thanks for trying to explain. I sort of see it but it isn't working out for me. I want to add say a class to my li items. You seem to be using ul for each item ? I don't see any sprintf in my code either so it's not clear. – landed Sep 9 '16 at 12:31
  • 1
    @landed This is for the whole menu, not for each item. Please use our search, we have existing solutions for your problem. – fuxia Sep 9 '16 at 13:27
  • I did search the web and others had the same issue I needed to extend the walker class and add my html there this worked ok for me but I had to copy quite a lot of code from the old class and rename it and change it then set the menu to use my custom walker class. Thanks – landed Sep 10 '16 at 17:29
  • I may be a bit late to the party but if you wanted to introduce text to items_wrap how would you i18n it? – henrywright Sep 19 '16 at 12:42
  • @henrywright Translate the values first, the pass them to the string. – fuxia Sep 19 '16 at 12:59

from what i gather it grabs an output and give the li a id and class with the menus name. So when you want to style a particular menu you grab its id and or class if you want to style it. the 1 and 2 is just a different output.

protected by fuxia Jun 8 '12 at 11:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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