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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to change CSS with jQuery?CSSinclude, include_once, require, require_once in PHPPHPCanvas Drawing in HTML5HTMLHow to show slider value in HTML5 range input using JavaScript?HTMLCreate a menu with a curtain falling animation using CSS and JavaScriptCSSIs Software Really Eating The World? I don’t think soMiscellaneousHow to make a UILabel clickable in Swift 5?SwiftForcing the domain to serve securely using HTTPSMiscellaneousHow to display random posts in WordPress?WordPressAdd animated hamburgers menu using Hamburgers collection on GitHubCSSArray.from() Method in JavaScriptJavaScriptHow to enable GD library support for PHP on windows server?PHP