Question

I have some fields which I only wish to show if the fields are not blank. My code so far…

<a href="<?php the_field('field_link'); ?>">
<img src="<?php the_field('field_image'); ?>"></a>

This works fine if someone has entered a value for both the link and image. Is it possible to not display anything if the fields are blank?

I have tried various configurations to make this work with my above code and can’t get it to work.

Was it helpful?

Solution

Since you're running PHP, it's as simple as wrapping all of that within a PHP if-block:

<?php if( get_field('field_link') && get_field('field_image') ): ?>
    <a href="<?php the_field('field_link'); ?>">
    <img src="<?php the_field('field_image'); ?>"></a>
<?php endif; ?>

Also, if you're unfamiliar, the above uses PHP's Alternative Syntax for control structures.

Edit: I understand now this is using the actual plugin Advanced Custom Fields and updated my code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top