Wordpress ACF Repeater Field - 仅拉入前 2 个字段
Wordpress ACF Repeater Field - Only pull in first 2 fields
主题有点说明了一切。我只需要拉入 ACF 转发器字段的前两个字段。
这是我正在尝试使用的,但显然不正确:
<?php $args = [ 'posts_per_page' => 2, 'order' => 'desc']; ?>
<?php $ar = new WP_Query( $args ); ?>
<?php if( $ar->have_rows('prodImgs') ): while ( $ar->have_rows('prodImgs') ) : $ar->the_row(); ?>
<img src="<?php the_sub_field('prodImg'); ?>" alt="">
<?php endwhile; ?>
<?php endif; ?>
我该怎么做?
<?php $rows = get_field('prodImgs'); ?>
<?php $i = 0; if ($rows):?>
<?php foreach($rows as $row): ?>
<img src="<?php echo $rows[$i][/* name of first field */]; ?>" alt="">
<?php echo $rows[$i][/* name of Second field */]; ?>
<?php $i++; endforeach; ?>
<?php endif; ?>
<?php
// check if the repeater has data
if( have_rows('prodImgs') ) {
//counter
$i=0;
//loop through the rows
while( have_rows('prodImgs') ) {
the_row();
//check if 2 subfields have been shown
if ( $i > 1 ) { break; }
echo "<img src='" . get_sub_field('prodImg_sub') . "' alt='Lorem ipsum'>";
$i++;
}
}
?>
您正在混合苹果和梨,WP_Query 和 ACF Repeater 字段。 WP_Query returns post 数据,同时 ACF 函数 have_rows( $repeater_field_name, $post_id );
检查您的 page/post 上的自定义字段转发器中是否有任何数据(或者如果您在相关 post/page 上指定 $post_id)。更多信息请见 https://www.advancedcustomfields.com/resources/repeater/
主题有点说明了一切。我只需要拉入 ACF 转发器字段的前两个字段。
这是我正在尝试使用的,但显然不正确:
<?php $args = [ 'posts_per_page' => 2, 'order' => 'desc']; ?>
<?php $ar = new WP_Query( $args ); ?>
<?php if( $ar->have_rows('prodImgs') ): while ( $ar->have_rows('prodImgs') ) : $ar->the_row(); ?>
<img src="<?php the_sub_field('prodImg'); ?>" alt="">
<?php endwhile; ?>
<?php endif; ?>
我该怎么做?
<?php $rows = get_field('prodImgs'); ?>
<?php $i = 0; if ($rows):?>
<?php foreach($rows as $row): ?>
<img src="<?php echo $rows[$i][/* name of first field */]; ?>" alt="">
<?php echo $rows[$i][/* name of Second field */]; ?>
<?php $i++; endforeach; ?>
<?php endif; ?>
<?php
// check if the repeater has data
if( have_rows('prodImgs') ) {
//counter
$i=0;
//loop through the rows
while( have_rows('prodImgs') ) {
the_row();
//check if 2 subfields have been shown
if ( $i > 1 ) { break; }
echo "<img src='" . get_sub_field('prodImg_sub') . "' alt='Lorem ipsum'>";
$i++;
}
}
?>
您正在混合苹果和梨,WP_Query 和 ACF Repeater 字段。 WP_Query returns post 数据,同时 ACF 函数 have_rows( $repeater_field_name, $post_id );
检查您的 page/post 上的自定义字段转发器中是否有任何数据(或者如果您在相关 post/page 上指定 $post_id)。更多信息请见 https://www.advancedcustomfields.com/resources/repeater/