Support » Plugin: Menu Item Custom Fields » Displaying field value in template

  • Resolved KennethDanielsen

    (@kennethdanielsen)


    Hi there,

    I’m having some trouble displaying my custom field’s value on a page.

    Getting the custom field to be displayed in the “Menus” works fine, but I’m not sure how to print out the value.

    I’ve created the following custom field:
    ( 'Image URL', 'imageurl' )

    How do I get my custom nav walker to display the value?

    Thanks in advance

    https://wordpress.org/plugins/menu-item-custom-fields/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Contributor Dzikri Aziz

    (@kucrut)

    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.

    Thread Starter KennethDanielsen

    (@kennethdanielsen)

    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:

    1. Installed the plugin
    2. Called include(‘menu-item-custom-fields-image.php’);
    3. Created the file above copying the default example
    4. Changed the custom field name using ( ‘Image URL’, ‘imageurl’ )
    5. Put text inside my new field in Menus
    6. 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

    Plugin Contributor Dzikri Aziz

    (@kucrut)

    Kenneth, it’d be helpful if you could post your code somewhere (preferably GitHub), so I can test it locally.

    Thread Starter KennethDanielsen

    (@kennethdanielsen)

    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

    Plugin Contributor Dzikri Aziz

    (@kucrut)

    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

    Thread Starter KennethDanielsen

    (@kennethdanielsen)

    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

    Thread Starter KennethDanielsen

    (@kennethdanielsen)

    Hi Dzikri,

    Did you see my message above?

    Best Regards
    Kenneth Danielsen

    Plugin Contributor Dzikri Aziz

    (@kucrut)

    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.

    Thread Starter KennethDanielsen

    (@kennethdanielsen)

    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 🙂

    Plugin Contributor Dzikri Aziz

    (@kucrut)

    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.

    Thread Starter KennethDanielsen

    (@kennethdanielsen)

    Hi Dzikri,

    Thanks a lot – I must have missed that 🙂

    It’s working perfectly now – once again, thumbs up for your support!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Displaying field value in template’ is closed to new replies.