Support

Account

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

Solving

Add a class to the first row

  • 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

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

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

You must be logged in to reply to this topic.