Hi! I’d like to share with you a new addon I have created. It allows you to add ACF fields to nav menu items.
For now it works only for ACF v.5+
The latest version you’ll find here https://github.com/DenisYakimchuk/ACF-Location-Nav-Menu/tree/master
It will appears on wordpress.org soon.
I think this is great, people have been looking for a way to do this for a while. The big question (or 2) I have is, where are the values stored and how do you use them.
without 3rd party plugins not possible to set filed for nav menu ?
@John Huebner
Menu items have the same fields and structure in db as any post type. So, data for menu items is stored with the same logic as it is done for post types. You don’t need add any special words near ID in second param of get_field / the_field functions. You can get values anywhere, just need to know menu item ID ( not “object_ID” ). But, mainly, I believe, it will be used in a Walker, for example for creating some Mega Menus.
without 3rd party plugins not possible to set filed for nav menu ?
No, ACF has no such location rule by default.
Will you be including examples of how to code and include custom Walker classes?
That would probably help out most people that would use it. I’m looking forward to giving it a try.
I have created a little wiki on github.
The addon is already available on wordpress.org
Welcome https://wordpress.org/plugins/location-nav-menu-for-acf/
Hi @happiness and @hube2
The plugin is wonderful, thank you @happiness !
But how to use in the menu function pulling the custom fields?
You have to build a custom menu walker, the author has an example of this included in the plugin https://github.com/DenisYakimchuk/ACF-Location-Nav-Menu/blob/master/inc/edit_custom_walker.php
@hube2 , It’s not an example. The file you referenced is a part of plugin. I do not recommend use it as a pattern. Simple example I’ve provided here https://github.com/DenisYakimchuk/ACF-Location-Nav-Menu/wiki/Examples
Thanks for the correction. I saw the that it was a copy of the walker class and assumed it could be used as an example.
Fantastic add-on to an already amazing plugin.
Is there a way to retrieve the ID of a Menu Item and print it in the HTML output? I’d like to allow the Admin user to simply select a checkbox with the LABEL ‘Enable Full Width’ on menu items within the Menu Manager, which I’ve created using the field type ‘Checkbox’ with field name ‘full_width_menu_item’. So far, so good.
Where I’m struggling is how to get the ID of menu items that have the check box enabled so that I can apply custom styling, e.g.
<style>
<?php [get the ID of each menu item with 'full_width_menu_item' selected]; ?> {
background:red;
}
</style>
so that the HTML output would be:
<style>
.menu-item-7465, .menu-item-1234 {
background:red;
}
</style>
Any help will be greatly appreciated.
Nice plugin, good job @happiness 🙂
Anyway we can use filters like this example:
add_filter('wp_nav_menu_items', 'my_wp_nav_menu_items', 10, 2);
function my_wp_nav_menu_items( $items, $args ) {
$menu = wp_get_nav_menu_object($args->menu);
$menuSlug = $menu->slug;
if ($menuSlug == 'hamburger-menu') {
if (is_user_logged_in()) {
$items .= '<li id="menu-item-logout" class="menu-item menu-item-logout"><a href="'. wp_logout_url(home_url()) .'">LOGOUT</a></li>';
} else {
$items .= '<li id="menu-item-login" class="menu-item menu-item-login"><a href="'. home_url() .'/logowanie">LOG IN</a></li>';
}
}
return $items;
}
This is an answer for related question “How to add items to an ACF Nav Menu?”
Hope it helps for future questions about it.
The topic ‘ACF Location "Nav Menu"’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.