You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Gutenberg+ACF在折叠面板中显示文章内容

问题:通过ACF的Post Object类型实现折叠面板,无法显示文章中的Gutenberg内容块

我用ACF的Post Object类型做了一个折叠面板,每个折叠项对应一篇文章。我在文章里插入了独立的ACF+Gutenberg内容块,但打开折叠面板时内容是空的,识别不到这些内容块。请问怎么让文章里的Gutenberg块正常显示?

当前使用的代码

<div class="accordion-block">
    <?php if (!empty($choose_acc_item)) : ?>
        <?php
        foreach ($choose_acc_item as $item) : ?>
            <div class="accordion-block__item">
                <button class="accordion-item__title"><?php echo $item->post_title; ?>
                </button>
                <div class="accordion-item__content">
                    <?php echo $item->post_content; ?>
                </div>
            </div>
    <?php
        endforeach;
    endif; ?>

</div>

代码检查器输出

输出值
此处本应显示我用ACF+Gutenberg制作的视频块内容。

解决方案

问题出在直接输出$item->post_content时,Gutenberg生成的块内容需要经过WordPress的内容过滤函数处理才能正确渲染。具体修改如下:

  1. 核心代码修改
    替换直接输出post_content的代码,改用apply_filters('the_content', $item->post_content),这个函数会触发WordPress的内容处理钩子,解析并渲染Gutenberg块:
<div class="accordion-item__content">
    <?php echo apply_filters('the_content', $item->post_content); ?>
</div>
  1. 额外注意事项
  • 确保$item是有效的WP_Post对象:ACF的Post Object字段默认返回WP_Post对象,若之前设置返回ID,需先用get_post($item_id)转换为对象。
  • 若使用ACF自定义块字段:比如文章里的ACF视频块字段,需用get_field('你的ACF字段名', $item->ID)获取值,再用apply_filters('the_content', $字段值)处理。

内容的提问来源于stack exchange,提问作者yanksi_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 14:25:29