【问题标题】:ACF flexible content layout group for table of contents用于目录的 ACF 灵活内容布局组
【发布时间】: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


【解决方案1】:

如果我正确理解您的问题,您只需要一个手风琴式下拉菜单来显示您的灵活内容段落部分的列表吗?

单手风琴式下拉内容本质上是一个锚链接列表,可直接导航到您的灵活内容段落部分。

如果是这种情况,请参阅下面的工作示例,了解如何实现这一点。

下面的所有代码都必须在您的 post 循环中运行。

<div class="container">

    <?php if(have_rows('paragraph')): ?>
        <div class="accordion" id="tableContentsAccordion">
            <div class="card">
                <div class="card-header" id="tableContents">
                    <h2 class="mb-0">
                        <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#tableContentsList" aria-expanded="true" aria-controls="tableContentsList">
                            Table of Contents
                        </button>
                    </h2>
                </div>
                <div id="tableContentsList" class="collapse show" aria-labelledby="tableContents" data-parent="#tableContentsAccordion">
                    <div class="card-body">
                        <?php while(have_rows('paragraph')): the_row();
                            $title = get_sub_field('title');
                            ?>
                            <a href="#<?php echo sanitize_title($title); ?>"><?php echo $title; ?></a><br/>
                        <?php endwhile; ?>
                    </div>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <?php if(have_rows('paragraph')): ?>
        <div class="row">
            <?php while(have_rows('paragraph')): the_row();
                $title = get_sub_field('title');
                ?>
                <div class="col-12" <?php echo $title ? 'id="' . sanitize_title($title) . '"' : ''; ?>>
                    <?php echo $title ? '<h2>' . $title . '</h2>' : ''; ?>
                    <?php the_sub_field('text'); ?>
                </div>
            <?php endwhile; ?>
        </div>
    <?php endif; ?>

</div>

正如前面关于您的问题的 cmets 所述,您需要运行两次 have_rows('paragraph') 语句,以便在两个单独的 html 位置输出灵活的内容段落部分内容。

实际上,您可以将我的所有代码合并到一个 have_rows('paragraph') 语句中,但前提是您的部分内容紧跟在您的目录 html 之后。

如果您想将目录放在侧边栏中或与段落部分输出分开的任何地方,则需要使用have_rows('paragraph') 两次。




更新 1

因此,为了回应您的第一条评论,我在测试环境中创建了这个示例 ACF 灵活内容字段。

灵活的内容字段名为paragraphs,字段位置设置为Post 帖子类型。

使用名为 paragraph 的单个块布局,带有两个子字段 titlename...


然后我用这个灵活的内容数据创建了一个新的post...

然后我在我的single.php 中运行了这个 php 代码...

<?php

// get our header
get_header();

?>

<main>
    <div class="container">

        <?php if(have_posts()): ?>

            <?php while(have_posts()) : the_post(); ?>

                <h1 class="pb-2 my-4 border-bottom"><?php the_title(); ?></h1>

                <?php if(have_rows('paragraphs')): ?>
                    <div class="accordion" id="tableContentsAccordion">
                        <div class="card">
                            <div class="card-header" id="tableContents">
                                <h2 class="mb-0">
                                    <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#tableContentsList" aria-expanded="true" aria-controls="tableContentsList">
                                        Table of Contents
                                    </button>
                                </h2>
                            </div>
                            <div id="tableContentsList" class="collapse show" aria-labelledby="tableContents" data-parent="#tableContentsAccordion">
                                <div class="card-body">
                                    <?php while(have_rows('paragraphs')): the_row(); ?>
                                        <?php $title = get_sub_field('title'); ?>
                                        <a href="#<?php echo sanitize_title($title); ?>"><?php echo $title; ?></a><br/>
                                    <?php endwhile; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if(have_rows('paragraphs')): ?>
                    <div class="row my-4">
                        <?php while(have_rows('paragraphs')): the_row(); ?>
                            <?php $title = get_sub_field('title'); ?>
                            <div class="col-12" <?php echo $title ? 'id="' . sanitize_title($title) . '"' : ''; ?>>
                                <?php echo $title ? '<h2>' . $title . '</h2>' : ''; ?>
                                <?php the_sub_field('text'); ?>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php endif; ?>

            <?php endwhile; ?>

        <?php endif; ?>

    </div>
</main>

<?php

// get our footer
get_footer();

这是&lt;main&gt; html 元素中的输出...

此更新代码与我的第一个代码示例相同,但在第二个更新示例中,我已将灵活内容字段名称从 paragraph 更改为 paragraphs,并且我还包含了完整的 singe.php 后循环。

不确定我的代码如何输出您的However 屏幕截图?

如果您需要更多帮助,请告诉我。

【讨论】:

  • 谢谢。我试过了,但我得到的输出与我的第三张截图(上图)完全相同,我说“但是,这就是我得到的:”
  • @chappers 看到更新 1在我的回答中,希望这可以使我的原始答案更加清晰。 ??
猜你喜欢
  • 1970-01-01
  • 2018-05-31
  • 2020-07-10
  • 2017-01-07
  • 2016-04-07
  • 2016-04-07
  • 2016-02-01
  • 2013-12-29
  • 2016-03-31
相关资源
最近更新 更多