php - 使用 word press 和 ACF 删除循环中的重复代码
问题描述
我有一个简单的引导手风琴,我还添加了一个在手风琴左侧显示图像的功能。
该图像显示了与手风琴相关的图像。
我应该左手风琴(col-6)和右图(col-6) 我知道它是我的循环,但我不确定最好的布局方式。
我目前有一个解决方案,我重复循环两次.. 这太糟糕了,我看不到清理它的方法。
有人可以指出我错在哪里以及如何简化这一点。
<div class="row-flex">
<?php
// check if the repeater field has rows of data
if (have_rows('accordion_repeater')):
$i = 1; // Set the increment variable
$ii = 1; // Set the increment variable
?>
<div class="accordion col" id="accordion">
<?php
// loop through the rows of data for the tab header
while (have_rows('accordion_repeater')) :
the_row();
$header = get_sub_field('accordion_header');
$content = get_sub_field('accordion_content');
$i++; // Increment the increment variable
?>
<div class="card mb-2">
<div class="card-header" id="heading-<?php echo $i; ?>">
<h4 class="tablinks p-2" id="defaultOpen" type="button" data-toggle="collapse"
data-target="#collapse-<?php echo $i; ?>"
aria-expanded="true"
aria-controls="collapse-<?php echo $i; ?>"
onclick="openCity(event, 'image-<?php echo $i; ?>')">
<?php echo $header; ?>
<i class="fa" aria-hidden="true"></i>
</h4>
</div>
<div id="collapse-<?php echo $i; ?>" class="collapse "
aria-labelledby="collapse-<?php echo $i; ?>" data-parent="#accordion">
<div class="card-body">
<?php echo $content; ?>
</div>
</div>
</div>
<?php
endwhile; //End the loop
?>
</div>
<div class=" col">
<?php
// loop through the rows of data for the tab header
while (have_rows('accordion_repeater')) :
the_row();
$image = get_sub_field('accordion_image');
$ii++; // Increment the increment variable
?>
<div id="image-<?php echo $ii; ?>" class="tabcontent">
<?php
if (!empty($image)): ?>
<img src="<?php echo esc_url($image['url']); ?>"
alt="<?php echo esc_attr($image['alt']); ?>"/>
<?php endif; ?>
</div>
<?php
endwhile; //End the loop
?>
</div>
<?php
else :
// no rows found
echo 'no content';
endif;
?>
</div>
解决方案
推荐阅读
- alexa - 为什么 Alexa 开发者控制台中的测试功能在 iPad Pro 上不起作用
- java - 仅当参数相同时,如何在不同线程上避免调用相同的方法
- google-data-studio - 按月和年聚合维度值
- sql - BATCH STATEMENT 中不允许的结果集 - SQL Anywhere
- laravel - 如何使用 Redis 在 Laravel 5.8 中调度异步作业?
- python - 在 groupby 之后折叠标题,每列具有不同的聚合
- snowflake-cloud-data-platform - 您可以将文件展平到 Snowflake 中的外部表吗?
- css - NgbDatePicker 在 html 表中未正确显示
- numpy - Numpy:在数组之前添加一些东西
- fhir-server-for-azure - 我们可以使用 Azure API for FHIR 在 FHIR 上的 SMART 范围内限制资源级别访问吗