【问题标题】:Advanced Custom field loop images高级自定义字段循环图像
【发布时间】:2017-01-14 00:46:23
【问题描述】:

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

 <?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; ?>

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    在 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; ?>
    

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

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

    wp_get_attachment_image_src() 返回一个数组。 [0] =&gt; url , [1] =&gt; width, [2] =&gt; height

    阅读文档以获取转发器字段here

    【讨论】:

    • 很好,但我不使用 ACF PRO
    • 抱歉,have_rows() 仅适用于 ACF(专业或高级插件)的中继器或灵活内容字段...所以我认为您会使用专业版...
    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 2015-07-07
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多