Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 3 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
add comment

Your Answer

 
discard

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.