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 badges1 Answer
Reset to default 2ACF 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
版权声明:本文标题:php - ACF: Only get first row of a Repeater Field 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741322355a2372277.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论