Wordpress ACF中继器字段 - 只需拉入前2个字段
问题描述:
主题有点说这一切。我只需要拉入ACF转发器字段的前两个字段。Wordpress ACF中继器字段 - 只需拉入前2个字段
这是我想使用,但它显然是不对的:
<?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
// 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直放站领域。 WP_Query返回发布数据,而ACF函数have_rows($repeater_field_name, $post_id);
检查页面/帖子上的自定义字段重复器中是否有任何数据(或者如果您在相关的帖子/页面上指定$ post_id)。更多信息在https://www.advancedcustomfields.com/resources/repeater/
答
<?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; ?>
你试图显示什么字段?当你说你只想要前两个字段时,这是否意味着中继器的前两行? – Jrod