Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?)

share|improve this question
add comment

2 Answers

up vote 30 down vote accepted

The parameter 'items_wrap' for wp_nav_menu() defaults to '<ul id="%1$s" class="%2$s">%3$s</ul>'. It 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.

share|improve this answer
    
Beautiful explanation. Thank you so much!! –  RodeoRamsey Jun 5 '11 at 13:39
add comment

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.

share|improve this answer
add comment

protected by toscho Jun 8 '12 at 11:46

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

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.