Ashley Cameron Design

Ashley Cameron Design

Shop the Creative Design Market

Good morning

Advanced Custom Fields – Loop through a repeater field

This post was last updated: Sep 3, 2020
SnippetsWordPress

About a 1 minute read

"Not until we are lost do we begin to understand ourselves."

Henry David Thoreau


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/* Loop through a repeater field
*  http://www.advancedcustomfields.com/resources/the_repeater_field/
*/


if(get_field('gallery_images')): ?>

    <?php while(the_repeater_field('gallery_images')): ?>
        <img src="<?php the_sub_field('image'); ?>" alt="<?php the_sub_field('alt'); ?>" />
    <?php endwhile; ?>

 <?php endif;

/*
* Loop through a repeater field from another post
* - Note that the_sub_field and get_sub_field don't need a second parameter
*/


$post_id = 123;
if( get_field('repeater_field_name', $post_id) )
{
    echo '<ul>';

    while( the_repeater_field('repeater_field_name', 5) )
    {
        echo '<li>sub_field_1 = ' . get_sub_field('sub_field_1') . ', sub_field_2 = ' . get_sub_field('sub_field_2') .', etc</li>';
    }

    echo '</ul>';
} ?>