Need help with PHP for get_post_meta
-
On a site, I’m using get_posts_meta to check if there are data in either of two custom fields and, if so, echo them on the screen.
What I originally put together was clunky and I’d like to improve it.
Here’s what I have:
<dl class="skills"> <!-- Get Required --> <?php if(get_post_meta($post->ID, "Required", true)): ?> <dt>Required:</dt> <dd class="required"><?php $key="Required"; echo get_post_meta($post->ID, $key, true); ?></dd> <?php endif; ?> <!-- End Required --> <!-- Get Preferred --> <?php if(get_post_meta($post->ID, "Preferred", true)): ?> <dt>Preferred:</dt> <dd class="preferred"><?php $key="Preferred"; echo get_post_meta($post->ID, $key, true); ?></dd> <?php endif; ?> <!-- End Preferred --> </dl>
This works great except when there are no custom fields with either key, in which case, an empty definition list displays.
I’d to test to see if either the Required or Preferred keys exist and, if so, include the definition list.
Problem I’m having is that I can’t nail down the correct PHP to accomplish this. I’d appreciate any help you can offer.
Thanks in advance,
Eric
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Need help with PHP for get_post_meta’ is closed to new replies.