the_repeater_field()

Overview

This function has been deprecated since v3.3.4, please user have_rows() instead.

Description

This function is used in a “while loop” to loop through each row of  a repeater field.  This function requires the repeater field.

This function will return either: the current row (continue loop) or false (end of loop).

Within the “while loop”, you can use these functions:

Parameters

<?php the_repeater_field($field_name, $post_id); ?>
  • $field_name: the name of the repeater field to be retrieved. eg “gallery_images” (required)
  • $post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

Usage

<?php

/*
* Loop through a 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>';
}

/*
*  $post_id examples
*/

$post_id = null; // current post
$post_id = 1;
$post_id = "option";
$post_id = "options"; // same as above
$post_id = "category_2"; // save to a specific category
$post_id = "event_3"; // save to a specific taxonomy (this tax is called "event")
$post_id = "user_1"; // save to user (user id = 1)

?>

Changelog

  • Deprecated in v3.3.4 – please replace with have_rows()
  • Added in v2.0.3

Related

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.