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 post with 4 custom fields, and I am using the_meta( ) to display these.
I want to add an image to the list items, but a different image for each item eg. key "Client" will have a small image of a person, key "Date" will have an image of a calendar...

The only way I can think of doing this is to somehow give each key a class and then use CSS to add a background image to the list-item.

How should I do this?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I think you have to output these Custom Fields manually to do it.

You can do it like so (let's assume your custom fields are: first-meta-key, second-meta-key, third-meta-key and you set them single value - no field has multiple values assigned to it):

<ul>
  <li><span class="first-post-meta-key">Name: </span> <?php echo esc_html(get_post_meta($post->ID, 'first-meta-key', true)); ?></li>
  <li><span class="second-post-meta-key">Name: </span> <?php echo esc_html(get_post_meta($post->ID, 'second-meta-key', true)); ?></li>
  <li><span class="third-post-meta-key">Name: </span> <?php echo esc_html(get_post_meta($post->ID, 'third-meta-key', true)); ?></li>
</ul>

Another way would be:

<?php $custom_fields = get_post_custom($post->ID); ?>
<ul>
<?php foreach ( $custom_fields as $key=>$values ): ?>
    <?php foreach ( $my_custom_field as $key => $value ): ?>
        <li><span class="<?php echo esc_attr($key); ?><?php echo esc_html($key); ?></span><?php echo esc_html($value); ?></li>;
    <?php endforeach; ?>
<?php endforeach; ?>

PS. It's not tested (and written directly in here) so can be a little bit buggy.

share|improve this answer
    
So to do it this way: <ul> <li><span class="first-post-meta-key">Name: </span> <?php echo esc_html(get_post_meta($post->ID, 'first-meta-key', true)); ?></li> <li><span class="second-post-meta-key">Name: </span> <?php echo esc_html(get_post_meta($post->ID, 'second-meta-key', true)); ?></li> <li><span class="third-post-meta-key">Name: </span> <?php echo esc_html(get_post_meta($post->ID, 'third-meta-key', true)); ?></li> </ul> I would just type the meta-key in the span and will the echo esc_html() part get the value? –  Adrian Jul 2 '13 at 11:22
    
get_post_meta($post->ID, '<META_KEY>', true) gets value of custom field <META_KEY> from post $post->ID –  Krzysiek Dróżdż Jul 2 '13 at 11:24
    
excellent, thank you, will try this out now. –  Adrian Jul 2 '13 at 11:37

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.