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