Support

Account

Home Forums Front-end Issues Help with object values within repeater field

Solving

Help with object values within repeater field

  • Hi, I’m new to ACF so my apologies for what is probably a very basic issue. I’ve tried browsing and implementing similar solutions found in the forums but I’m still struggling.

    I have an advanced loop for a repeater field, however I’m struggling to call in the object values for an icon post object which is a sub_field associated to each of the repeater fields as a required object. Hoping you can advise please on where I’m going wrong. Here is my code:

    
    <?php if( have_rows('unique_selling_points') ): ?>
    
    		<div class="wrapper">
    
    			<div class="usp-list equal-heights">
    
    				<?php
    				$n = 0;
    				while( have_rows('unique_selling_points') ): the_row();
    					$n++;
    
    					// vars
    					$icon = get_sub_field('icon');
    					$heading = get_sub_field('heading');
    					$intro = get_sub_field('intro');
    					$text = get_sub_field('text');
    					?>
    
    					<article class="unit">
    						<?php
    						if($icon):
    							?>
    				            <i class="has-circle bg-brand2">
    			                    <svg class="icon-<?php echo $icon['css']; ?>">
    			                        <use xlink:href="<?php echo get_template_directory_uri(); ?>/images/icons/icons.svg#icon-<?php echo $icon['css']; ?>"></use>
    			                    </svg>
    			                </i>
    			        		<?php
    				        endif;
    				        ?>
    						<h1 class="heading4"><?php echo $heading; ?></h1>
    						<p class="intro"><?php echo $intro; ?></p>
    						<?php if( $text ): ?>
    							<div id="usp-<?php echo $n?>" class="reveal">
    								<?php echo $text; ?>
    							</div>
    							<a href="javascript:void(0);" title="Read more" class="btn hover2">
    								Read more
    								<i class="down">
    				                    <svg class="icon-arrow-down">
    				                        <use xlink:href="<?php echo get_template_directory_uri(); ?>/images/icons/icons.svg#icon-arrow-down"></use>
    				                    </svg>
    				                </i>
    							</a>
    						<?php endif; ?>
    					</article>
    
    				<?php endwhile; ?>
    
    			</div>
    
    		</div>
    
    	<?php endif; ?>
    
  • This is for a Post Object field $icon = get_sub_field('icon');?

    What are the settings for the field? What are you returning?

    is $icon['css'] a custom field on this other post object?

  • Hi John,

    Correct, $icon['css'] is a custom field for the icon custom post object I have created.

  • The post object only returns the “post” information similar to what is available in “The Loop”. In ACF 5 you can choose to return just the post ID or an object, so that can change things. Either way, the custom fields related to the post object that is returned are not part of what’s returned. You need to use ACF function to get those values. For more information see the documentation for Post Object fields here https://www.advancedcustomfields.com/resources/post-object/

  • Hey John,

    Thanks for the advice. I’ve changed my code to the below, and the icon now works, however the loop seems to break after the first record. There should be 3 records, but I’m now only getting one record in the loop. What am I missing or doing wrong?

    
    <?php if( have_rows('unique_selling_points') ): ?>
    
    		<div class="wrapper">
    
    			<div class="usp-list equal-heights">
    
    				<?php
    				$n = 0;
    				while( have_rows('unique_selling_points') ): the_row();
    					$n++;
    
    					$icon = get_sub_field('icon');
    					$heading = get_sub_field('heading');
    					$intro = get_sub_field('intro');
    					$text = get_sub_field('text');
    					?>
    					<article class="unit">
    						<?php
    						if ($icon):
    							$post = $icon;
    							setup_postdata($post);
    							?>
    							<i class="has-circle bg-brand2">
    			                    <svg class="icon-<?php the_field('css'); ?>">
    			                        <use xlink:href="<?php echo get_template_directory_uri(); ?>/images/icons/icons.svg#icon-<?php the_field('css'); ?>"></use>
    			                    </svg>
    			                </i>
    						    	<?php
    						    wp_reset_postdata();
    						endif;
    				        ?>
    						<h1 class="heading4"><?php echo $heading; ?></h1>
    						<p class="intro"><?php echo $intro; ?></p>
    						<?php if( $text ): ?>
    							<div id="usp-<?php echo $n?>" class="reveal">
    								<?php echo $text; ?>
    							</div>
    							<a href="javascript:void(0);" title="Read more" class="btn hover2">
    								Read more
    								<i class="down">
    				                    <svg class="icon-arrow-down">
    				                        <use xlink:href="<?php echo get_template_directory_uri(); ?>/images/icons/icons.svg#icon-arrow-down"></use>
    				                    </svg>
    				                </i>
    							</a>
    						<?php endif; ?>
    					</article>
    
    				<?php endwhile; ?>
    
    			</div>
    
    		</div>
    
    	<?php endif; ?>
    
  • More than likely this has to do with the wp_reset_postdata(); call. Reset post data resets it to the main query.

    I’m guessing that you have a secondary query in the template. This happens when you get more than two nested queries in WP.

    There are two methods you can use to fix this

    
    if ($icon):
      // hold the current post
      $temp_post = $post;
    	$post = $icon;
    	setup_postdata($post);
    	?>
    	<i class="has-circle bg-brand2">
    							<svg class="icon-<?php the_field('css'); ?>">
    									<use xlink:href="<?php echo get_template_directory_uri(); ?>/images/icons/icons.svg#icon-<?php the_field('css'); ?>"></use>
    							</svg>
    					</i>
    			<?php
    		//wp_reset_postdata(); don't do this
    		// instead do this
    		$post = $temp_post;
    endif;
    

    OR

    
    if ($icon):
    	$post = $icon;
    	// do not do this
    	// setup_postdata($post);
    	// instead use values in the $icon object
    	// see changes to get_field() calls
    	?>
    	<i class="has-circle bg-brand2">
    							<svg class="icon-<?php the_field('css', $icon->ID); ?>">
    									<use xlink:href="<?php echo get_template_directory_uri(); ?>/images/icons/icons.svg#icon-<?php the_field('css', $icon->ID); ?>"></use>
    							</svg>
    					</i>
    			<?php
    		// do not do this
    		//wp_reset_postdata();
    endif;
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Help with object values within repeater field’ is closed to new replies.