Home Forums General Issues Show content if link field is not filled


Show content if link field is not filled

  • Hello,
    I have an image field and a link field.

    I want the image to link to some page within the site.
    I managed to do that. The problem comes when, because of the ‘if’ statement of the link, if the link is not filled, the image is hidden.

    How can I manage so if there’s no link, there are no pointer events and if there’s link it links to the selected page?


    Here’s my code:

    	<?php if (get_sub_field('type') == "Image") { ?>
    							<div class="full-img">
    																							$link = get_sub_field('link');
    																							if( $link ): ?>
    							<a href="<?php echo esc_url( $link ); ?>">
    										<img src="<?php the_sub_field('image'); ?>">
    										<p class="caption"><?php the_sub_field('caption'); ?>
    																						<?php endif; ?>
    						<?php } ?>
      if (get_sub_field('type') == "Image") {
          <div class="full-img">
              $link = get_sub_field('link');
              $content = '<img src="'.get_sub_field('image').'">';
              if ($link) {
                $content = '<a href="'.esc_url($link).'">'.$content.'</a>';
              echo $content;
            <p class="caption"><?php the_sub_field('caption'); ?></p>
  • Thanks a lot! It worked

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Show content if link field is not filled’ is closed to new replies.