Hi Kenneth,
You can get the value just like you’d get a value of a post custom field:
$value = get_post_meta( $item_id, 'imageurl', true );
where $item_id
is the ID of menu item.
Hey Dzikri,
Thanks for your quick reply :-).
I’m not sure what I’m doing wrong though, it doesn’t seem to work. Here’s what I’ve done:
- Installed the plugin
- Called include(‘menu-item-custom-fields-image.php’);
- Created the file above copying the default example
- Changed the custom field name using ( ‘Image URL’, ‘imageurl’ )
- Put text inside my new field in Menus
- Declared the field content in my Custom_Nav_Walker:
$item_output .= '<br /><img src="' . get_post_meta( $item->ID, 'imageurl', true ) . '"/>';
Still nothing happens. When I try using var_dump I just get empty strings for each menu item. The walker ends up printing “<img src>”.
Best Regards
Kenneth Danielsen
Kenneth, it’d be helpful if you could post your code somewhere (preferably GitHub), so I can test it locally.
Hi Dzikri,
Thanks a lot for your help – I’ve added the relevant files on this repository:
https://github.com/KennethDanielsen/Menu-Item-Custom-Fields-test
Thanks for the code Kenneth. It was my bad. The example plugin I provided in the plugin is missing some important parts 🙂
I’ve made some changes to your code and posted it as a gist: https://gist.github.com/kucrut/6065fdcbf4e0f9cebfb9
Hi Dzikri,
Thanks a lot – your help is really appreciated :-).
It seems to be working as expected now! I get a weird bug though.
When adding new menu items, it adds a link at the bottom saying “Please try again”, and the menu item was not added. The bug is gone as soon as I comment out the included menu-item-custom-fields file.
I’ve uploaded a GIF here, for you to see the bug in action. My installation is in Danish, so I apologize any inconvenience.
See the GIF here: http://gfycat.com/NimbleLinearBelugawhale
Hi Dzikri,
Did you see my message above?
Best Regards
Kenneth Danielsen
Hi Kenneth,
I just released a new version of the plugin a few hours ago. Please update and use the new example plugin. I couldn’t reproduce your issue on my site so please try it without any other plugins activated and using the default theme.
Hi Dzikri,
Awesome job, I just updated to 0.3.0 and the bug is gone! Also I dig the way you made it easier to add more fields in just one line per field.
Using the new version I seem to have some trouble printing out the fields in my custom walker, though. I’m calling the field using:
<?php get_post_meta( $item->ID, 'imageurl', true ); >'
,
which still gives me nothing in my template.
I’ve tried outputting $item using var_dump, which doesn’t give me any of my custom fields in the object.
On a side note, it would be nice to be able to set conditions for which menu item types the field should be displayed (pages, links, categories etc.).
Thanks for your support on this plugin 🙂
Kenneth,
If you take a look at the new example, you’ll see that the meta keys are prefixed with menu-item-
. So, to get the value:
get_post_meta( $item->ID, 'menu-item-imageurl', true );
Regarding conditions, I was in fact working on a project that requires me to only show the custom fields when the menu is set to a location. I’ll see if I can find some free time next week to make a blog post for it.
Hi Dzikri,
Thanks a lot – I must have missed that 🙂
It’s working perfectly now – once again, thumbs up for your support!