Call custom field into menu item

I have a navigation menu in WordPress. I would like it to check if the page has a custom field of “menu_icon” and if it does, add that custom field into the menu item it corresponds to. I’m sure this would require some sort of walker function extension but I am a bit new to PHP and WordPress. I’ve search around and have not found a suitable tutorial.

For example:
(normal menu)

<ul>
   <li><a href="#">link</a></li>
</ul>

(custom)
<ul>
   <li><span class="icon" style="background-image:url(CUSTOM FIELD CALLED HERE);"></span><a href="link">link</a></li>
</ul>

Related posts

Leave a Reply

2 comments

  1. This is what i have used for checking to see if the custom field is there or not. I am sure you can use it as well.

    <?php 
        $custom_field = get_post_meta($post->ID, 'Your Custom Field Name', true);
    
        // Checks to see if there is a value in the custom field
        if($custom_field != '') { echo $custom_field; }
    ?>
    

    It probably would look something like this with your code above:

    <ul>
       <li><span class="icon" style="background-image:url(<?php 
        $custom_field = get_post_meta($post->ID, 'Your Custom Field Name', true);
    
        // Checks to see if there is a value in the custom field
        if($custom_field != '') { echo $custom_field; }
    ?>);">   </span><a href="link">link</a></li>
    </ul>