对于我正在创建的自定义WP主题,我使用ACF。我需要一个输出图像的中继器。我把它设置为图像对象,并使用正确的代码。事实上,我在没有中继器的情况下尝试过,效果很好。只有在中继器内它才会失败。
<div class="row col-md-12">
<?php
// check if the repeater field has rows of data
if( have_rows('pics') ):
// loop through the rows of data
while ( have_rows('pics') ) : the_row();
// display a sub field value
?><div class="col-md-4">
<?php
$image = get_field('img');
if( !empty($image) ): ?>
<img src="<?php echo $image; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div> <?
endwhile;
else :
// no rows found
endif;
?>
</div>
是什么导致图像数据不循环?
您的代码看起来不错,但检查了我的操作方式后,我认为您应该使用get_sub_field
而不是get_field
http://www.advancedcustomfields.com/resources/get_sub_field/
您关于使用数组的['url']部分的评论也是相关的。这对我很有效;
$image = get_sub_field('img');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
我认为这就是你必须做的:
<?php
// check if the repeater field has rows of data
if( have_rows('img') ):
// loop through the rows of data
while ( have_rows('img') ) : the_row();
// display a sub field value
$image = get_sub_field('sub_img'); ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endwhile;
else :
// no rows found
endif;
?>
这是假设您输出的是一个图像对象,而不是ACF设置中的url。