Say "Hello!"

  • This field is for validation purposes and should be left unchanged.

Advanced Custom Fields – Loop through a repeater field

Web Hosting at Bluehost Affiliate Disclosure

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>';
} ?>

Back to Snippets Follow me on GitHub

Ashley Cameron logo
Blog