admin管理员组文章数量:1318129
How do I add custom fields to a specific menu?
For example, I have menu "Primary" with ID 13. How do I run the wp_nav_menu_item_custom_fields
hook only if ID matches the currently selected menu?
I tried getting the ID from the $id
parameter of the hook, but it always returns 0. I'm trying to check the ID like this:
function add_menu_field( $item_id, $item, $depth, $args, $id ) {
var_dump( $id );
}
add_action( 'wp_nav_menu_item_custom_fields', 'add_menu_field', 10, 5 );
This returns int(0)
but my nav menu ID is 13. Am I using this hook incorrectly, or is there another way to get the ID? My goal is to put custom fields on a specific nav by checking the ID.
Thanks for any help!
How do I add custom fields to a specific menu?
For example, I have menu "Primary" with ID 13. How do I run the wp_nav_menu_item_custom_fields
hook only if ID matches the currently selected menu?
I tried getting the ID from the $id
parameter of the hook, but it always returns 0. I'm trying to check the ID like this:
function add_menu_field( $item_id, $item, $depth, $args, $id ) {
var_dump( $id );
}
add_action( 'wp_nav_menu_item_custom_fields', 'add_menu_field', 10, 5 );
This returns int(0)
but my nav menu ID is 13. Am I using this hook incorrectly, or is there another way to get the ID? My goal is to put custom fields on a specific nav by checking the ID.
Thanks for any help!
Share Improve this question asked Nov 14, 2020 at 23:29 Justin BreenJustin Breen 134 bronze badges 2 |1 Answer
Reset to default 1Am I using this hook incorrectly
No, you're not.
is there another way to get the ID?
Yes, try this:
function add_menu_field( $item_id, $item, $depth, $args, $id ) {
$id2 = 0;
if ( ! $id && isset( $_GET['menu'] ) && $_GET['menu'] ) {
$id2 = absint( $_GET['menu'] );
}
elseif ( ! $id && ! isset( $_GET['menu'] ) ) {
// Get recently edited nav menu.
$id2 = absint( get_user_option( 'nav_menu_recently_edited' ) );
if ( ! is_nav_menu( $id2 ) ) {
$nav_menus = wp_get_nav_menus();
$id2 = ( ! empty( $nav_menus ) ) ? $nav_menus[0]->term_id : 0;
}
}
if ( 13 === $id2 && is_nav_menu( $id2 ) ) {
echo '<hr>add your awesome custom field here';
}
}
And that's basically how WordPress gets the ID on the wp-admin/nav-menus.php
(Appearance → Menus) page — if there's no selected menu ($_GET['menu']
is not set), we use the recently edited menu, if any; if none, we use the first menu in the available nav menus.
Alternate Option (use a filter to add the action)
You can use wp_edit_nav_menu_walker
to add your action to wp_nav_menu_item_custom_fields
whereby the action (i.e. the hook callback) would simply display the custom field. For example:
add_filter( 'wp_edit_nav_menu_walker', 'wpse_378190', 10, 2 );
function wpse_378190( $class, $menu_id ) {
$tag = 'wp_nav_menu_item_custom_fields';
if ( 13 === $menu_id && ! has_action( $tag, 'add_menu_field2' ) ) {
add_action( $tag, 'add_menu_field2', 10, 5 );
}
return $class;
}
// This function is hooked to wp_nav_menu_item_custom_fields and fires only if the
// menu ID is 13. So no need to check the $id and just display your custom field.
function add_menu_field2( $item_id, $item, $depth, $args, $_id ) {
$id = 13;
echo '<hr>display your awesome custom field here';
}
本文标签: Add custom fields to specific menus
版权声明:本文标题:Add custom fields to specific menus 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741993586a2409596.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if ( 13 === $id ) { // add your field }
. – Sally CJ Commented Nov 15, 2020 at 0:27$id
if it always returns 0. – Justin Breen Commented Nov 15, 2020 at 0:54