-
-
Save helgatheviking/497b12b18d14db4d0d8bb917fb08a737 to your computer and use it in GitHub Desktop.
Thanks for this!
You're welcome. Complete blog post here: https://www.kathyisawesome.com/add-custom-fields-to-wordpress-menu-items/
If I want to add custom fields to specific menus (not all menus), how do I do that?
@photocurio the Menu itself does not seem to be passed anywhere as an argument to the wp_nav_menu_item_custom_fields
hook so conditional logic based on the menu seems unlikely. It might maybe be in the $args
array... you'd have to check.
My advice would be to leave the fields and just ignore them.
@helgatheviking, its true the fields can be overlooked, but if you have a lot of menus, and some menus dedicated for specific purposes, and a lot of data, things can get messy. I'll post if I find a solution.
@photocurio Fair. I'd try adding additional parameters, var dumping the new params and see what they are:
function kia_custom_fields( $item_id, $item, $args, $menu_id ) {
var_dump($args);
var_dump($menu_id);
}
add_action( 'wp_nav_menu_item_custom_fields', 'kia_custom_fields', 10, 4 );
You might get lucky and have the menu location in the $args
array.
Neat example. Thank you!
How would I actually show an ACF custom field such as "Location" (which is the address for the Location) in addition to the post title In this area of Wordpress menus??? I need to show the Home Listings location not just the Post Title which is a basic name of the home. Any ideas would be helpful...Then on the frontend in the drop down menu, it would say for example: Northwood • 555 TheCity, State Zip
Sorry @whynotadv I don't know the answer to that. Does the nav_menu_item_title
not apply in the admin?
Thanks @helgatheviking for replying. It might, but I'm not sure. Looks like probably have to use is_admin () https://developer.wordpress.org/reference/functions/is_admin/
I think this is something close, but not sure it's exactly what I'm looking for???? https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/
Again, not sure if the wp_get_nav_menu_items
in run at this point in the admin either... you'll have to test.
This is a really nice, minimal example for https://make.wordpress.org/core/2020/02/25/wordpress-5-4-introduces-new-hooks-to-add-custom-fields-to-menu-items/. Thanks for writing this up!