Support » Themes and Templates » Hide article image if empty

  • Resolved Asitis

    (@asitis)


    Hey,
    I use Custom Fields on my weblog to post a article image next to each post. Now on some posts I dón’t want a article image. But if I leave the custom field empty, it still displays the container of the image. I know it’s possible to hide the complete container if there is no content for it, but I don’t know how!
    This is the code I use for article images:

    <?php //get article_image (custom field) ?><?php $image = get_post_meta($post->ID, 'article_image', true); ?><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><div id="artimg_container"><img class="artimg" src="<?php echo $image; ?>" width="110" height="110" alt="<?php the_title(); ?>" align="right"/></div></a>

Viewing 3 replies - 1 through 3 (of 3 total)
  • Jeremy Clark

    (@jeremyclark13)

    Member

    You could try this

    <?php //get article_image (custom field) ?>
    <?php $image = get_post_meta($post->ID, 'article_image', true); ?>
    <?php if (!empty($image)) {;?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
    <div id="artimg_container"><img class="artimg" src="<?php echo $image; ?>" width="110" height="110" alt="<?php the_title(); ?>" align="right"/></div></a>
    <?php }; ?>

    This will test if the $image variable is not empty and display the rest of the command if it is empty ie no image then nothing is displayed. Keep in mind I haven’t tested this.

    Thanks mate! This works just perfectly fine! 🙂

    Hi, I am having a similar issue as Asitis, but my code is different. I too am displaying an image from the custom fields, and wish to not display it if the image was not inserted in the custom field (as some posts simply don’t go with an image). This is my code:

    <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 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Hide article image if empty’ is closed to new replies.