Yogesh Chauhan's Blog

WordPress: How to display slider repeater fields in ACF?

in WordPress on February 27, 2021

The Repeater field is a really good solution for repeating content like slides, team members, directory, and many more repeating contents.

The Repeater field type acts as a parent to a set of sub fields which can be repeated again and again and there are no limits to the number of repeats either as long as you don’t specify.

The Repeater field will return an array of rows, where each row is an array containing sub field values.

When you add a repeater field, it creates additional functions specifically for looping over rows and accessing sub field values like have_rows, the_row, get_sub_field, and the_sub_field functions.

How to loop through a Repeater field and generate the HTML for a basic image slider?



<?php if( have_rows('slides') ): ?>
    <ul class="slides">
    <?php while( have_rows('slides') ): the_row(); 
        $image = get_sub_field('image');
        ?>
        <li>
            <?php echo wp_get_attachment_image( $image, 'full' ); ?>
            <p><?php the_sub_field('caption'); ?></p>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>


How to manually loop over a Repeater field value using a foreach loop?



<?php 
$rows = get_field('repeater_field_name');
if( $rows ) {
    echo '<ul class="slides">';
    foreach( $rows as $row ) {
        $image = $row['image'];
        echo '<li>';
            echo wp_get_attachment_image( $image, 'full' );
            echo wpautop( $row['caption'] );
        echo '</li>';
    }
    echo '</ul>';
}



Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Number Properties in JavaScriptJavaScriptHow to Use Aggregate Functions (MIN, MAX, SUM, AVG, COUNT) to Summarize Data in SQL?SQL/MySQLHow to CREATE TABLE in SQL with and without using Another Table?SQL/MySQLHow to create a circle that follows a cursor using JavaScript and CSS?CSSfirst-of-type and last-of-type selectors in CSSCSSHow to Recognize an Array in JavaScript?JavaScriptSQL Inner JoinSQL/MySQL2 Ways We Can Write Multiple Line Commands in PHPPHPHow to Validate User Name, Email Address and URL in PHP?PHP4 ways to create Date Objects in JavaScriptJavaScriptThe substr() method in JavaScript and how it’s different from substring()JavaScriptEffects in ReactReact