WordPress.org

Forums

If custom field is empty show a default image (4 posts)

  1. Hiperciencia
    Member
    Posted 7 years ago #

    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"

  2. esmi
    Forum Moderator
    Posted 7 years ago #

    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" />';
    ?>
  3. Hiperciencia
    Member
    Posted 7 years ago #

    Thank you esmi, but it is returning this url:

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

  4. marujobhz
    Member
    Posted 6 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic