Cthulhu
Cthulhu

Reputation: 5235

How to hide "image" custom field if empty

I created an unordered list of custom fields and I wish to hide them if they are empty. For text custom fields I used the code:

<?php if (get_field('phone') != '') { ?>
  <li><strong>Phone: </strong><?php the_field('phone'); ?></li>
<?php } ?>

However, I have a custom field which is for images, like this:

<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>

How can I hide the field if no image was uploaded (obviously, the above code won't work)? Thanks in advance.

Upvotes: 0

Views: 4046

Answers (3)

Ashwani Panwar
Ashwani Panwar

Reputation: 4568

<?php if( get_field('field_name') ): ?>
<p>My field value: <?php the_field('field_name'); ?></p>

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86406

I think it should be

<?php if (get_field('logo') != ''): ?>
      <li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php endif; ?>

Upvotes: 5

Alex Turpin
Alex Turpin

Reputation: 47776

Assuming the_field('logo') will return a falsy value if there are no images

if (the_field('logo')) {
    ?>
        <li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
    <?php
}

Upvotes: 3

Related Questions