主题有点说明了一切。我只需要拉入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; ?>
我该怎么做?
答案 0 :(得分:1)
<?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返回发布数据,而ACF函数have_rows( $repeater_field_name, $post_id );
检查页面/帖子上的自定义字段转发器中是否有任何数据(或者如果您在相关帖子/页面上指定$ post_id)。有关详情,请访问https://www.advancedcustomfields.com/resources/repeater/
答案 1 :(得分:0)
<?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; ?>