Reputation: 1220
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.
Upvotes: 0
Views: 1019
Reputation: 8077
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.
Upvotes: 2