admin管理员组

文章数量:1287968

I have a repeater filed called "images" and inside it I have a sub_filed called "image".

How can I get only the first row value?

Here is my code

<?php if( have_rows('images') ): ?>
<?php
$active = 'active';
while ( have_rows('images') ) : the_row();
$image = get_sub_field('image');
if($image == "" ) continue;
?>
<img src="<?php echo get_template_directory_uri(); ?>/files/images/<?php the_sub_field('image'); ?>" class="img-fluid is-slider-item" />
<?php $active = '';
endwhile;
?>
<?php endif; ?>

What I tried

<?php
$active = 'active';
while ( have_rows('images') ) : the_row();
$image = get_sub_field('image');
if($image == "" ) continue;
?>
<img src="<?php echo get_template_directory_uri(); ?>/files/images/<?php the_sub_field('image'); ?>" class="img-fluid is-slider-item" />
break;
<?php $active = '';
endwhile;
?>
<?php endif; ?>

Many thanks in advance!

I have a repeater filed called "images" and inside it I have a sub_filed called "image".

How can I get only the first row value?

Here is my code

<?php if( have_rows('images') ): ?>
<?php
$active = 'active';
while ( have_rows('images') ) : the_row();
$image = get_sub_field('image');
if($image == "" ) continue;
?>
<img src="<?php echo get_template_directory_uri(); ?>/files/images/<?php the_sub_field('image'); ?>" class="img-fluid is-slider-item" />
<?php $active = '';
endwhile;
?>
<?php endif; ?>

What I tried

<?php
$active = 'active';
while ( have_rows('images') ) : the_row();
$image = get_sub_field('image');
if($image == "" ) continue;
?>
<img src="<?php echo get_template_directory_uri(); ?>/files/images/<?php the_sub_field('image'); ?>" class="img-fluid is-slider-item" />
break;
<?php $active = '';
endwhile;
?>
<?php endif; ?>

Many thanks in advance!

Share Improve this question asked Sep 13, 2021 at 17:12 SilberArtSilberArt 211 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

ACF has a function called get_row_index() that you can utilize. Here is how you can possibly use it in your case

    <?php
      $active = 'active';
      while ( have_rows('images') ) : the_row();
      $image = get_sub_field('image');
      if(get_row_index() == '1' ):
    ?>
      <img src="<?php echo get_template_directory_uri(); ?>/files/images/<? 
 php the_sub_field('image'); ?>" class="img-fluid is-slider-item" />
    break;
    <?php $active = '';
    endwhile;
    ?>
    <?php endif; ?>

Notice I checked to see if the row index was 1 (the first row) then displayed what was needed within that logic.

Hope this helps!

本文标签: phpACF Only get first row of a Repeater Field