0

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')) ?>" />
4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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