Support

Account

Home Forums Add-ons Repeater Field Add a class to the first row

Solving

Add a class to the first row

    • gcmatt

    • February 1, 2021 at 8:20 am

    Hi all,

    I’m using a repeater field to output a list of images for a slideshow.

    I need to add a class to the first image in the array so that I can make that first image visible when the page loads. Looking at the docs, I suspect I need to use get_row_index(), but I’m not too sure how. Here’s my code as it stands:

    
    	<?php if ( have_rows( 'images' ) ) : ?>
    		<?php while ( have_rows( 'images' ) ) : the_row(); ?>
    			<?php $image = get_sub_field( 'image' ); ?>
    			<?php if ( $image ) : ?>
                    <div class="slide" style="background-image: url('<?php echo esc_url( $image['url'] ); ?>;')">
                        <?php the_sub_field( 'description' ); ?>
                    </div>
    			<?php endif; ?>
    		<?php endwhile; ?>
    	<?php else : ?>
    		<?php // no rows found ?>
    	<?php endif; ?>

    Can anyone point me in the right direction?

    Matt

    • gcmatt

    • February 2, 2021 at 9:53 am

    I think I’ve answered my own question. This seems to do the job for me:
    <?php if ( $rows[0] ) : ?>classNameHere<?php endif; ?>

  • I would use get_row_index()

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

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.