The wp_nav_menu_items
filter has a secondary parameter that it passes: $args
. What we need to do is set up our filter to accept second args by passing a priority 10
and the number of args 2
:
add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu', 10, 2 );
Now that we’re passing args – if we peek into what it holds we see the following object:
stdClass Object
(
[menu] => Menu Name
[container] => nav
[container_class] =>
[container_id] => mainNav
[menu_class] => menu
[menu_id] =>
[echo] => 1
[fallback_cb] => wp_page_menu
[before] =>
[after] =>
[link_before] =>
[link_after] =>
[items_wrap] => <ul id="%1$s" class="%2$s">%3$s</ul>
[depth] => 0
[walker] =>
[theme_location] =>
)
So if we wanted to in your function we can test against either $args->menu
or $args->theme_location
:
if( 'Menu Name' === $args->menu ) {
/* .. Run Code Here .. */
}
Or against the location:
if( 'primary-menu' === $args->theme_location ) {
/* .. Run Code Here .. */
}