【问题标题】:ACF Image in Repeater not working中继器中的 ACF 图像不起作用
【发布时间】:2015-06-12 08:36:15
【问题描述】:

对于我正在创建的自定义 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>

是什么导致图像数据不循环?

【问题讨论】:

  • 也适用于 img 标签“echo $image['url'];”也不能解决问题。

标签: php wordpress plugins repeater advanced-custom-fields


【解决方案1】:

您的代码看起来不错,但是在检查了我是如何做到的之后,我相信您应该使用 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; ?>

【讨论】:

    【解决方案2】:

    我认为这是你必须做的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2014-09-22
      • 1970-01-01
      • 2018-04-17
      • 2022-12-05
      • 2017-10-03
      • 2018-04-02
      相关资源
      最近更新 更多