我无法使用高级自定义字段 WordPress 插件从中继器获取图像

I can't get images from a repeater using Advanced Custom Fields WordPress plugin

我附上了中继器的屏幕截图和图像高级自定义字段。

我已经为转发器图片块上传了三张图片。

现在,我可以获得那些船,但无法在屏幕上查看。我打开我的检查元素,我看到 while 循环正在执行并显示所有三艘船代码。

我的代码:

        <?php 
/* 
Template Name: boatProduct 
*/ ?>

<?php get_header(); ?>

            <?php if( have_rows('boat_product_slider') ): while ( have_rows('boat_product_slider') ) : the_row(); ?>
            <div class="product_boat" style="background: url('<?php the_sub_field('slider_image'); ?>'); background-size: cover;">

            </div>
            <?php endwhile; endif; ?>

        </div>

        <div id="charter" class="brokerage" style="background: linear-gradient(rgba(255,255,255,0.95), rgba(255,255,255,0.95)), url('<?php the_field('agys_icon'); ?>') no-repeat center 48%; background-size: 80%;  background-attachment: fixed;">


        <h3 style="margin-top:40px;">Specification</h3>
        <p align="justify"><?php the_field('content'); ?></p>
    </div>

<?php get_footer(); ?>

您需要对代码稍作改动,将 the_sub_field() 替换为 get_sub_field()

来自 get_sub_field() 文档:

This function will return a sub field value from a repeater field or flexible content field loop. This function is used within a have_rows() loop.

the_sub_field() 函数将打印结果。这对您不起作用,因为您已将 slider_image 设置为 return "Image Object"。

来自文档:

This function will display a sub field value from a repeater field or flexible content field loop. This function is used within a have_rows() loop.

This function is the same as echo get_sub_field('name');.


编辑:"The Loop"

关于如何处理 WordPress 循环,您有两种选择:

选项 1

ACF函数需要在WordPress loop内部使用。例如(使用默认的 TwentySixteen WordPress 主题中的 page.php

<?php
// Start the loop.
while ( have_posts() ) : the_post();


    // ACF functions should be added here, inside "the Loop"


    // Include the page content template.
    get_template_part( 'template-parts/content', 'page' );

    // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) {
        comments_template();
    }

    // End of the loop.
endwhile;
?>

选项 2

或者,您可以将 $post_id 参数添加到 have_rows() 以指定 ACF 字段应来自哪个 post/page:

$post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

$post_id = 123; // This should be the ID of the post/page that contain the ACF fields
if( have_rows('boat_product_slider', $post_id) ):

    // loop through the rows of data
    while ( have_rows('boat_product_slider', $post_id) ) : the_row();
        // ...