Support

Account

Home Forums Add-ons Repeater Field Need basic help with accessing repeater array

Solved

Need basic help with accessing repeater array

  • Hi – i’m not really sure what i’m doing wrong here. Seems very basic and yet I can’t access the items inside of the array.

    Field name is ‘post_links’ and my sub-field is called ‘items’ and references WordPress post objects.

    I can print_r($rows) and see the full array of data which looks something like:
    Array ( [0] => Array ( [items] => WP_Post Object ( [ID] => 15582 [post_author] => 2 [post_date] => 2010-07-29 13:51:40

    Just not sure how to grab the stuff inside of it, can anybody help?

    <?php 
    $rows = get_field('post_links');
    if($rows)
    {
    	echo '<ul>';
     
    	foreach($rows as $row)
    	{
    		echo '<li>sub_field_1 = ' . $row['items'] . '</li>';
    	}
     
    	echo '</ul>';
    }
    ?>
  • I’m assuming you are doing this within the loop and you don’t have to set up postdata. With that said, since you have a post object field inside a repeater, you dont have to use the foreach loop.

    <?php while(has_sub_field('post_links')): ?>
    	<?php $post_objects = get_sub_field('items'); ?>
    	<ul>
    	    <li>
    			<a href="<?php echo get_permalink($post_objects->ID); ?>"><?php echo get_the_title($post_objects->ID); ?></a>
    
    		</li>
    	</ul>
    <?php endwhile; ?>
  • Thanks a lot Mike!

    That’s far more efficient and had a “feeling” i was doing something not quite right. But makes perfect sense now.

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

The topic ‘Need basic help with accessing repeater array’ is closed to new replies.