Skip to main content
added 196 characters in body
Source Link
Rarst
  • 99.9k
  • 10
  • 160
  • 298

wp_nav_menu_items filters passes menu arguments as second parameter.

So if you modify code to this:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

function my_custom_menu_item($items, $args) ...

You will be able to check data $args inside function to match specific menu. However the specifics of what to check for depend on how precisely your menus are set up. Might be $args['menu'] holding name, might be $args['theme_location'], etc.

Another option is to simply add your callback right before the needed menu is called and remove right after. in theme template:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');

// target wp_nav_menu() call in between

remove_filter( 'wp_nav_menu_items', 'my_custom_menu_item');

wp_nav_menu_items filters passes menu arguments as second parameter.

So if you modify code to this:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

function my_custom_menu_item($items, $args) ...

You will be able to check data $args inside function to match specific menu. However the specifics of what to check for depend on how precisely your menus are set up. Might be $args['menu'] holding name, might be $args['theme_location'], etc.

Another option is to simply add your callback right before the needed menu is called and remove right after.

wp_nav_menu_items filters passes menu arguments as second parameter.

So if you modify code to this:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

function my_custom_menu_item($items, $args) ...

You will be able to check data $args inside function to match specific menu. However the specifics of what to check for depend on how precisely your menus are set up. Might be $args['menu'] holding name, might be $args['theme_location'], etc.

Another option is to simply add your callback right before the needed menu is called and remove right after in theme template:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');

// target wp_nav_menu() call in between

remove_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
Source Link
Rarst
  • 99.9k
  • 10
  • 160
  • 298

wp_nav_menu_items filters passes menu arguments as second parameter.

So if you modify code to this:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

function my_custom_menu_item($items, $args) ...

You will be able to check data $args inside function to match specific menu. However the specifics of what to check for depend on how precisely your menus are set up. Might be $args['menu'] holding name, might be $args['theme_location'], etc.

Another option is to simply add your callback right before the needed menu is called and remove right after.