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?)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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_idis the parameter'menu_id'if you have it set, else it is'menu-' . $menu->slug.$wrap_classis the parameter'menu_class'if you have it set, else it is empty.$itemsis 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.
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0