I'm using the following code to display a custom value. The custom value being 'website'

  $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


  • 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

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).

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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