WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code. When There is only one image in the backend, the second image shows up with a little question mark instead of nothing. I am looking for a way to check if the image exists first and if so run the code and if not don't.

<?php $image = wp_get_attachment_image_src(get_field('post_image1'), 'thumbnail'); ?>
        <img src="<?php echo $image[0]; ?>" alt="<?php get_the_title(get_field('post_image1')) ?>" />
        <?php $image = wp_get_attachment_image_src(get_field('post_image2'), 'thumbnail'); ?>
        <img src="<?php echo $image[0]; ?>" alt="<?php get_the_title(get_field('post_image2')) ?>" />
share|improve this question
up vote 4 down vote accepted

wp_get_attachment_image_src will return false if there is no image. So before you echo the image add if ($image !=false)

share|improve this answer
Thanks Works great – Anders Kitson Jun 12 '12 at 21:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.