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'm using the following code to display a custom value. The custom value being 'website'

<?php
  $custom_fields = get_post_custom($post_id); //Current post id
  $my_custom_field = $custom_fields['website']; //key name
  foreach ( $my_custom_field as $key => $value )
  echo $key . " => <a href='" . $value . "'>Click Here</a><br />";
?>

However, before the 'Click here' i'd like to include another custom value, thumb.

So the output would be:

Value of custom field thumb
Value of custom field website

Thanks.

share|improve this question
    
So i've managed to come up with a workaround. Used two similar functions, one above the other. Maybe this isn't the right way, so if anyone has a cleaner suggestion, let me know. <?php $custom_fields = get_post_custom($post_id); //Current post id $my_custom_field = $custom_fields['thumb']; //key name foreach ( $my_custom_field as $key => $value ) echo "<img src='" . $value . "'>"; ?> Which did the trick –  Sledge81 Aug 4 '11 at 17:59

1 Answer 1

up vote 2 down vote accepted

How about:

$thumb = get_post_meta( $post_id, 'thumb', true );
$url = get_post_meta( $post_id, 'website', true);
echo "<a href='$url'><img src='$thumb' /></a>";

The get_post_meta() function is ideal when you know ahead-of-time the names of the fields you want to retrieve. Setting the third parameter to true tells it that you want to return a single value, and not an array of values (it's possible to set multiple values using the same meta key, and get an array of values back).

share|improve this answer
    
What happens if one of those values comes back as empty? Might i suggest ... if( $thumb && $url ) prior to the echo. –  t31os Aug 5 '11 at 9:20

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.