Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
add comment

2 Answers

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>
share|improve this answer
add comment

You can use the get_post_meta function:

<?php 
$value = get_post_meta($post->ID, 'CUSTOM_FIELD_NAME', true);

if($value != '') {
  echo $value;
} 
?>

Read more about it here: http://codex.wordpress.org/Function_Reference/get_post_meta

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.