简体   繁体   English

WordPress 高级自定义字段,循环中继器

[英]WordPress advanced custom fields, loop though repeater

What I have我有的

I have a repeater setup in Advanced custom fields and bootstrap slider in my code.我在高级自定义字段中有一个中继器设置,并在我的代码中引导 slider。

What I trying to achieve我想要达到的目标

I would like to add a image and text in my WordPress repeater and have it populate my slider.我想在我的 WordPress 中继器中添加图像和文本,并让它填充我的 slider。 so that each new repeater row has its own image and text.这样每个新的中继器行都有自己的图像和文本。

I have the image working, but not the text so at the moment every slide has its own image, but for some reason I have all the text displaying for each slide.我有图像工作,但不是文本,所以目前每张幻灯片都有自己的图像,但由于某种原因,我为每张幻灯片显示了所有文本。

bellow is the layout of my ACF repeater下面是我的 ACF 中继器的布局

在此处输入图像描述

bellow is my current slider下面是我目前的 slider

so I have 3 images that show on the correct slides but as you can see my content is showing all not just the content related for the slide所以我有 3 张图片显示在正确的幻灯片上,但正如您所见,我的内容显示的不仅仅是与幻灯片相关的内容

在此处输入图像描述

My code我的代码

<?php
$sliderImages = get_field('section_8_slider');
$count = 0;
$section = "section_8_";
$order = array();
?>
 <div class="col-lg-8 mb-3">
                <div class="carousel-inner">
                    <?php foreach ($sliderImages as $imgNumber => $image): ?>
                        <div class="carousel-item<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
                            <img src="<?= $image['image']['url'] ?>" alt="<?= $image['image']['alt'] ?>">

                            <?php if (have_rows($section . 'slider')):
                                while (have_rows($section . 'slider')):
                                    the_row(); ?>
                                    <?php

                                    foreach ($sliderImages as $i => $row)
                                    if ($sliderImages ): ?>
                                            <div class="carousel-text-block">
                                                <h4 class="mb-1"> <?php echo $row['content_title']; ?></h4>
                                                <p class="small m-0"> <?php echo $row['content']; ?></p>
                                            </div>
                                    <?php endif; ?>
                                <?php endwhile;
                            endif; ?>

                        </div>
                    <?php endforeach ?>
                </div>
                <a class="carousel-control-prev" href="#carousel_03" role="button" data-slide="prev">
                    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                    <span class="sr-only"></span>
                </a>
                <a class="carousel-control-next" href="#carousel_03" role="button" data-slide="next">
                    <span class="carousel-control-next-icon" aria-hidden="true"></span>
                    <span class="sr-only"></span>
                </a>
            </div>

You are looping each field 2 times.您将每个字段循环 2 次。 You could use get_sub_field() in order to simplify your code, try something like this:您可以使用get_sub_field()来简化您的代码,尝试这样的事情:

<div class="carousel-inner">
<?php if (have_rows('section_8_slider')):
    while (have_rows('section_8_slider')): the_row();
        $image = get_sub_field('image');
        $content_title = get_sub_field('content_title');
        $content = get_sub_field('content');?>
        <div class="carousel-item">
            <img src="<?= $image['url'] ?>" alt="<?= $image['alt'] ?>">
            <div class="carousel-text-block">
                <h4 class="mb-1"> <?= $content_title;?></h4>
                <p class="small m-0"> <?= $content;?></p>
            </div>
        </div>
    <?php endwhile;
endif; ?>

Hope it helps希望能帮助到你

I have managed to solve it myself, i was close, please see answer bellow我已经设法自己解决了,我很接近,请看下面的答案

<div class="carousel-inner">
                    <?php foreach ($sliderImages as $imgNumber => $image): ?>
                        <div class="carousel-item<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
                            <img src="<?= $image['image']['url'] ?>" alt="<?= $image['image']['alt'] ?>">
                            <div class="carousel-text-block">
                                <h4 class="mb-1"> <?php echo $image['content_title']; ?></h4>
                                <p class="small m-0"> <?php echo $image['content']; ?></p>
                            </div>
                        </div>
                    <?php endforeach ?>
                </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WordPress高级自定义字段-高级中继器 - Wordpress Advanced Custom Fields - Advanced Repeater Wordpress 高级自定义字段中继器在循环外显示标题 - Wordpress Advanced Custom Fields repeater display title outsite of loop WordPress高级自定义字段-按子字段值的随机查询中继器 - WordPress Advanced Custom Fields - Random Query Repeater by SubField value WordPress 中的高级自定义字段 - 无法在组内使用中继器 - Advanced Custom Fields within WordPress - Unable to use a repeater within a group WordPress高级自定义字段中继器,每3个div换行 - WordPress Advanced Custom Fields Repeater, wrap every 3 divs in a row 在Wordpress高级自定义字段PRO中向嵌套转发器添加行 - Add row to nested repeater in Wordpress Advanced Custom Fields PRO 高级自定义字段中继器-如何反转顺序(WordPress) - Advanced Custom Fields Repeater - How to reverse order (WordPress) 高级自定义字段WordPress中继器字段按日期排序 - Advanced Custom Fields Wordpress Repeater Field Order By Date 使用高级自定义字段插件和转发器字段在Wordpress中制作幻灯片 - Making a Slideshow in Wordpress using the Advanced Custom fields plugin and the repeater field 中继器中的高级自定义字段所见即所得 - Advanced Custom Fields wysiwyg in repeater
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM