0

I'm using Wordpress and the advanced custom fields plugin to create gallery thumbnails. There are 9 possible upload fields, but only the first is required.

At present I am having an issue where any of the fields are empty it is still outputting the img tag but showing a corrupt image (as there is no image).

I need to create an if statement that only outputs the HTML if the field has a src (or if the field isn't empty).

This is my current code, there are 9 of these:

<li>
    <div class="portfolioPicker">
       <?php $image = wp_get_attachment_image_src(get_field('portfolio_image_2'), 'thumbnail'); ?>
       <img id="portfolioImg_2" class="portfolioPickerThumb" src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('portfolio_image_2')) ?>" />
    </div>
</li>

I have found a good piece of code for the PHP if statement but am struggling to get it working with my current output code:

<?php if( get_post_meta($post->ID, "mycustomfieldname", true) ): ?>
<?php echo get_post_meta($post->ID, "mycustomfieldname", $single = true);?>
<?php else: ?>

<?php endif; ?>

1 Answer 1

2

Here are three possible solutions to only display the tag if the array key is set:

<li>
    <div class="portfolioPicker">
    <?php
            $image = wp_get_attachment_image_src(get_field('portfolio_image_2'), 'thumbnail');
            if($image[0] != NULL)
            {
    ?>
                <img id="portfolioImg_2" class="portfolioPickerThumb" src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('portfolio_image_2')) ?>" />
    <?php
            }
    ?>
    </div>
</li>

OR

<li>
    <div class="portfolioPicker">
    <?php
            $image = wp_get_attachment_image_src(get_field('portfolio_image_2'), 'thumbnail');
            if(is_null($image[0]) == false)
            {
    ?>
                <img id="portfolioImg_2" class="portfolioPickerThumb" src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('portfolio_image_2')) ?>" />
    <?php
            }
    ?>
    </div>
</li>

OR

<li>
    <div class="portfolioPicker">
    <?php
            $image = wp_get_attachment_image_src(get_field('portfolio_image_2'), 'thumbnail');
            if(empty($image[0]) == false)
            {
    ?>
                <img id="portfolioImg_2" class="portfolioPickerThumb" src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('portfolio_image_2')) ?>" />
    <?php
            }
    ?>
    </div>
</li>

Which function you use and how you use it will depend on the empty value itself - what IS "empty"? i.e. '', 0, ' ', NULL could all mean empty.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.