带ACF中继器字段的Bootstrap 4 Carosuel

时间:2021-08-13 作者:vopakor

给定这段HTML代码,如何使用引导程序4创建带有缩略图的动态Carosuel,该引导程序4可以使用ACF Repeater在后端进行更新?

我的中继器字段被称为carousel_repeater 在里面我有一个图像区域叫做carosuel_image

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div id="custCarousel" class="carousel slide" data-ride="carousel" align="center">
                <!-- slides -->
                <div class="carousel-inner">
                    <div class="carousel-item active"> <img src="https://i.imgur.com/weXVL8M.jpg" alt="Hills"> </div>
                    <div class="carousel-item"> <img src="https://i.imgur.com/Rpxx6wU.jpg" alt="Hills"> </div>
                    <div class="carousel-item"> <img src="https://i.imgur.com/83fandJ.jpg" alt="Hills"> </div>
                    <div class="carousel-item"> <img src="https://i.imgur.com/JiQ9Ppv.jpg" alt="Hills"> </div>
                </div> <!-- Left right --> <a class="carousel-control-prev" href="#custCarousel" data-slide="prev"> <span class="carousel-control-prev-icon"></span> </a> <a class="carousel-control-next" href="#custCarousel" data-slide="next"> <span class="carousel-control-next-icon"></span> </a> <!-- Thumbnails -->
                <ol class="carousel-indicators list-inline">
                    <li class="list-inline-item active"> <a id="carousel-selector-0" class="selected" data-slide-to="0" data-target="#custCarousel"> <img src="https://i.imgur.com/weXVL8M.jpg" class="img-fluid"> </a> </li>
                    <li class="list-inline-item"> <a id="carousel-selector-1" data-slide-to="1" data-target="#custCarousel"> <img src="https://i.imgur.com/Rpxx6wU.jpg" class="img-fluid"> </a> </li>
                    <li class="list-inline-item"> <a id="carousel-selector-2" data-slide-to="2" data-target="#custCarousel"> <img src="https://i.imgur.com/83fandJ.jpg" class="img-fluid"> </a> </li>
                    <li class="list-inline-item"> <a id="carousel-selector-2" data-slide-to="3" data-target="#custCarousel"> <img src="https://i.imgur.com/JiQ9Ppv.jpg" class="img-fluid"> </a> </li>
                </ol>
            </div>
        </div>
    </div>
</div>

1 个回复
SO网友:DeltaG

您是否查看了高级自定义字段文档?您可以在此处检查中继器字段的工作方式:https://www.advancedcustomfields.com/resources/repeater/

您将看到,可以执行以下操作在前端显示ACF中继器图像:

if ( have_rows(\'carousel_repeater\') ) : 

    while ( have_rows(\'carousel_repeater\') ) : the_row();

        $yourImage = get_sub_field(\'carosuel_image\');
        
        if ( $yourImage ) : 

            echo wp_get_attachment_image( $yourImage, \'full\' );

        endif;

    endwhile;

endif;
上面的代码段假定图像字段设置为ID。如果图像字段有其他返回类型,请参阅https://www.advancedcustomfields.com/resources/image/.