• Hi.

    I am displaying an image on each one of my posts which I load from a custom field. However, not all posts are accompanied by an image and I’d like to not display the image if the custom field is empty, meaning that there is no image to display. This is the code I use to display the image from the custom fields:

    <a rel="lightbox[hemingway_block]" href="<?php $values = get_post_custom_values("image07urlLarge"); echo $values[0]; ?>"><img class="imgblock" src="<?php $values = get_post_custom_values("image07urlThumb"); echo $values[0]; ?>" alt="<?php the_title(); ?>" /></a>

    What code could I use to hide the image if there is no data in the ‘image07urlLarge’ custom field?

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Not sure if this will help but this is what I use on author.php to show custom fields. If the field is empty it won’t show anything.

    <?php
    if (!$curauth->phone_number == '')
    { ?>
    Phone Number:
    <br />
    <?php echo $curauth->phone_number; ?>
    <br /><br />
    <?php } ?>

    orjuela, you can do it like this:

    <?php
    if(!empty(get_post_custom_values("image07urlLarge")){
    ?>
    <a rel="lightbox[hemingway_block]" href="<?php $values = get_post_custom_values("image07urlLarge"); echo $values[0]; ?>"><img class="imgblock" src="<?php $values = get_post_custom_values("image07urlThumb"); echo $values[0]; ?>" alt="<?php the_title(); ?>" /></a>
    <?php
    }
    ?>

    All I did was surround the whole thing in an if statement – it says that if that custom field is not empty, display the code, if not, just skip it.

    Hope it helps, 6 months later…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hiding image if custom field empty’ is closed to new replies.