Is there a way to determine which theme placement a menu is for when modifying it with the wp_get_nav_menu_items
filter hook?
I’d like to only add items to certain menus and I don’t want to have to depend on the ID or the name of the menu.
1 Answer
Not sure this is the simplest means, but you could conditionally add the filter to just the location you want to modify via wp_nav_menu_args
filter when wp_nav_menu
is called, then immediately remove the filter so it isn’t applied to other menus.
function wpa108544_nav_menu_args( $args ){
// check if it's the location we want the filter applied to
if( 'primary' == $args['theme_location'] )
add_filter( 'wp_get_nav_menu_items', 'wpa108544_get_nav_menu_items', 10, 3 );
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpa108544_nav_menu_args' );
function wpa108544_get_nav_menu_items( $items, $menu, $args ){
// remove the filter
remove_filter( current_filter(), __FUNCTION__, 10, 3 );
// do your menu manipulation stuff here
}