基本上我正在尝试做的是循环转发器字段(ACF,在WordPress中工作)以挑出1个子字段,并且在循环中的第一个项目之后运行第二个循环以获取另一个子字段领域。
例如,如果我有一个带有两个子字段“title”和“content”的转发器,输出将如下所示:
<div class="row">
<div class="column">
<div class="tabs-title">
<h3>Item 1 Title</h3>
<a href="#panel1">Learn More</a>
</div>
</div>
<div class="column">
<div class="tabs-title">
<h3>Item 2 Title</h3>
<a href="#panel2">Learn More</a>
</div>
</div>
<div class="column">
<div class="tabs-title">
<h3>Item 3 Title</h3>
<a href="#panel3">Learn More</a>
</div>
</div>
<div class="tabs-panel" id="panel1">
<div class="tabs-content">
<p>Item 1 content goes here.</p>
</div>
</div>
<div class="tabs-panel" id="panel2">
<div class="tabs-content">
<p>Item 2 content goes here.</p>
</div>
</div>
<div class="tabs-panel" id="panel3">
<div class="tabs-content">
<p>Item 3 content goes here.</p>
</div>
</div>
</div>
<!-- Repeat loop until the end -->
到目前为止,这就是我所拥有的。只是不确定如何在每隔3个选项关闭div之前执行第二个循环。
<?php if( have_rows('services') ) :
$i = 1;
$divopen = '<div class="cta row row-3 small-up-1 large-up-3 tabs" data-tabs id="example-tabs">';
echo $divopen;
while( have_rows('services') ) : the_row(); ?>
<div class="column">
<div class="tabs-title">
<h3><?php the_sub_field('service_name'); ?></h3>
<a href="#panel<?php echo $i; ?>">Learn More</a>
</div>
</div>
<?php if($i % 3 == 0) :
echo '</div>' . $divopen;
endif;
$i++; endwhile; echo '</div>';
endif; ?>
答案 0 :(得分:0)
希望你能找到正确的解决方案,你可以遍历一个数组并创建一个这样的表结构(这个答案是根据我的理解):
$array = [
[
[
'title' => "My Title 1",
'content' => 'Content 1',
],
[
'title' => "My Title 2",
'content' => 'Content 2',
],
[
'title' => "My Title 3",
'content' => 'Content 3',
],
],
[
[
'title' => "My Title 4",
'content' => 'Content 4',
],
[
'title' => "My Title 5",
'content' => 'Content 5',
],
],
];
echo "<table style='border: 1px solid black;'><tbody>";
foreach ($array as $key => $arr) {
echo "<tr> . '<br>'";
foreach ($arr as $key => $value) {
echo "<td style='border: 1px solid black;'>";
echo $value['title'];
echo $value['content'];
echo '</td>';
}
echo "<tr>";
}
echo "</tbody></table>";
希望这有帮助!