高级自定义场循环图像

Advanced Custom field loop images

我需要在高级自定义字段中显示循环。此代码return只有第一张图片。

 <?php if( have_rows('colors') ): ?>

    <ul>

    <?php while( have_rows('colors') ): the_row(); ?>

    <?php   $image = wp_get_attachment_image_src(get_field('colori'), 'full'); ?>
<img src="<?php echo $image; ?>" alt="<?php echo get_the_title(get_field('colors')) ?>" />

    <?php endwhile; ?>

    </ul>

<?php endif; ?>

在 ACF 中继器字段中,您必须使用 get_sub_field(),而不是 get_field()。所以你的代码应该是这样的:

<?php if( have_rows('colors') ): ?>

    <ul>

    <?php while( have_rows('colors') ): the_row(); ?>

    <?php   $image = wp_get_attachment_image_src(get_sub_field('colori'), 'full'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('colors')) ?>" />

    <?php endwhile; ?>

    </ul>

<?php endif; ?>

它可能再次 returns 错误值,因为我不知道你是如何命名你的 ACF(中继器)子字段的。

子字段'colori'必须是输出ID的ACF图片字段。不是数组或其他东西。

wp_get_attachment_image_src() returns 一个数组。 [0] => url , [1] => width, [2] => height

阅读转发器字段的文档 here