Home Forums Add-ons Repeater Field Showing a sub field if it exists


Showing a sub field if it exists

    	<div class="credits-container">
    		<section class="entry-content content-projects" itemprop="articleBody">
    				<?php if(get_field('credit')): ?>
    					<h2 class="centre credits">Credits</h2>
    					<?php while(has_sub_field('credit')): ?>
    						<p class="centre credits">
    							<?php the_sub_field('role'); ?>: 
    							<a href="">
    								<?php the_sub_field('name'); ?>
    					<?php endwhile; ?>
    				<?php endif; ?>

    I am using the above code to display a repeater that has 3 sub fields; Role, Name and Link.

    There won’t always be a link, which is a URL that will be wrapped around the name.

    I am struggling to get the link to show if there is one, as I know my current code just displays the link no matter what. I had tried something but to no avail, so any help would be much appreciated.

    Thank you

    $name = get_sub_field('name');
    if (get_sub_field('link')) {
        <a href="<?php the_sub_field('link'); ?>"><?php echo $name; ?></a>
    } else {
      echo $name;
  • This is great John, thank you!

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

The topic ‘Showing a sub field if it exists’ is closed to new replies.