Support

Account

Home Forums Front-end Issues Link not showing if Custom Field is not empty

Solved

Link not showing if Custom Field is not empty

  • Hi,

    I’m trying to create an if-else statement for one of my pages.

    Basically, there’s a repeater field and it has subfields (Heading, Image, Link).

    I’m trying to add an <a> tag to the container if the link subfield is populated and remove it if it’s empty. However, the <a> tag isn’t working/showing even if it’s not empty.

    <?php if(get_field('listing_link', $ik)): ?>
     <a href="<?php the_sub_field('listing_link',$ik);?>">
    <?php endif; ?>
    
    <div class="blok-low">
    	<div class="lt">
    		<?php $attachment_idiconlist = get_sub_field('listing_icon');
    			$sizeiconlist = "practicesublisticonimage"; // (thumbnail, medium, large, full or custom size)
    			$imageiconlist = wp_get_attachment_image_src( $attachment_idiconlist, $imageiconlist);
    		?>
    		<img src="<?php echo $imageiconlist[0]; ?>" alt=""  />
    	</div>
    	<div class="txt">
    	<?php the_sub_field('listing_heading');?>
    	</div>
    </div>
    <?= (get_field('listing_link', $ik)) ? '</a>' : ''; ?>
  • Hi,

    I found the answer to my problem. I was trying to get the the_field when the listing_link is a sub_field.

    <?php if(get_sub_field('listing_link' ,$ik)): ?>
    	<a href="<?php the_sub_field('listing_link'); ?>">
    <?php endif; ?>
    <div class="blok-low">
    	<div class="lt">
    		<?php $attachment_idiconlist = get_sub_field('listing_icon');
    			$sizeiconlist = "practicesublisticonimage"; // (thumbnail, medium, large, full or custom size)
    			$imageiconlist = wp_get_attachment_image_src( $attachment_idiconlist, $imageiconlist);
    		?>
    		<img src="<?php echo $imageiconlist[0]; ?>" alt=""  />
    	</div>
        <div class="txt">
            <?php the_sub_field('listing_heading');?>
        </div>
    </div>
    <?php echo (get_sub_field('listing_link', $ik)) ? '</a>' : '';?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Link not showing if Custom Field is not empty’ is closed to new replies.