WordPress.org

Support

Support » How-To and Troubleshooting » If custom field is empty show a default image

If custom field is empty show a default image

  • Does anybody knows if there is a code, that i can use in the template pages, that reads the custom field to show the image and, if the custom field is empty, show a default image?

    Today I use:

    <img src=”<?php $values = get_post_custom_values(“ImageDestaque”); echo $values[0]; ?>” alt=”” style=”border:#000000 1px solid” />

    I would like to show a “/blank.gif” in case of an empty custom field “ImageDestaque”

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    @esmi

    Forum Moderator

    How about:

    <?php
    if(get_post_custom_values("ImageDestaque")) {
    	$values = get_post_custom_values("ImageDestaque");
    	$my_url = $values[0];
    }
    else $my_url = '/blank.gif';
    echo '<img src="<?php echo $my_url;?>" alt="" style="border:#000000 1px solid" />';
    ?>

    Thank you esmi, but it is returning this url:

    http://hypescience.com/%3C?php%20echo%20$my_url;?%3E

    hi. i need to instead use a blank.gif, if customfield is empty dont show nothing. because IE display a blank error image (red X). in FF its ok, nothing appears. but in IE this error image appears. thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘If custom field is empty show a default image’ is closed to new replies.