【发布时间】:2022-10-06 05:57:20
【问题描述】:
我有一个 ACF 灵活内容字段,其布局名称为 \'post_section\',其中有一个名为 \'paragraph\' 的组字段,其中包含 \'id\'、\'title\' 和 \'text\' 的字段。在我的 component-post_section.php 文件中,我有以下 php:
<?php if( have_rows(\'paragraph\') ): ?>
<?php while( have_rows(\'paragraph\' ) ): the_row(); ?>
<div class=\"container\">
<div class=\"row\" id=\"<?php the_sub_field(\'id\'); ?>\">
<h2><?php the_sub_field(\'title\'); ?></h2>
<?php the_sub_field(\'text\'); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
这会产生以下结果:
但我试图制作以下内容 - 上面的引导手风琴目录,其中包含两个 post_section 标题的标题:
这是我的PHP:
<?php if( have_rows(\'paragraph\') ): ?>
<?php while( have_rows(\'paragraph\' ) ): the_row(); ?>
<div id=\"accordion\">
<div class=\"card\">
<div class=\"card-header\" id=\"headingOne\">
<h5 class=\"mb-0\">
<button class=\"btn btn-link\" data-toggle=\"collapse\" data-target=\"#collapseOne\" aria-expanded=\"true\" aria-controls=\"collapseOne\">
Table of Contents
</button>
</h5>
</div>
<div id=\"collapseOne\" class=\"collapse show\" aria-labelledby=\"headingOne\" data-parent=\"#accordion\">
<div class=\"card-body\">
<p><a href=\"<?php the_sub_field(\'id\'); ?>\"><?php the_sub_field(\'title\'); ?></a></p>
</div>
</div>
</div>
</div>
<div class=\"container\">
<div class=\"row\" id=\"<?php the_sub_field(\'id\'); ?>\">
<h2><?php the_sub_field(\'title\'); ?></h2>
<?php the_sub_field(\'text\'); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
但是,这就是我得到的:
关于如何实现我想要的任何想法,即每个段落的标题和文本字段字段的输出,但只有一个像这样的目录 div 中的标题输出?
也许我需要为手风琴创建一个单独的组件 php 文件?
谢谢你的帮助。
-
您将需要 2 个
while循环。第一个打印目录,第二个打印手风琴 -
我试过了,但它不起作用。
-
你是什么意思它不工作?
-
你能编辑我的代码并给我答案吗?我尝试在我认为需要的地方添加两个 while 循环,但我显然错了,因为它只是不断重复每个段落的目录。
-
您使用的是什么引导程序版本?
标签: php advanced-custom-fields acfpro