Forums

Hiding image if custom field empty (3 posts)

  1. orjueladiego
    Member
    Posted 3 years ago #

    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!

  2. bustherh
    Member
    Posted 3 years ago #

    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 } ?>
  3. Peter Butler
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic