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

注册ACF块时如何启用块嵌套?配置后无法插入子块

问题:ACF自定义包装块无法插入内部块?

我想创建一个包装块(wrapper block),让用户能把多个块放进同一个包装里,不用每个块单独加包装。我想用PHP实现,已经按照网上的指南给ACF注册的块启用了内部块(inner blocks),也加了要求的'jsx' => true配置,但编辑器还是不允许把其他块插入这个包装块里。

WP Gutenberg列表视图

注册块的PHP代码:

<?php

/**
 * Create custom blocks here.
 */
function theme_blocks() {
  $blocks = [];

  $blocks[] = [
    'name' => 'general',
    'title' => 'General',
    'mode' => 'edit',
    'supports' => ['mode' => false, 'align' => false]
  ];

  $blocks[] = [
    'name' => 'page-section',
    'title' => 'Page Section',
    'mode' => 'edit',
    'supports' => ['jsx' => true, 'mode' => false, 'align' => false, 'anchor' => true]
  ];

  return $blocks;
}

/**
 * Loop through defined blocks from theme_blocks and register them.
 */
function theme_register_blocks() {
  foreach ( theme_blocks() as $block ) {
    $block['render_template'] = "template-parts/blocks/{$block['name']}.php";
    acf_register_block_type( $block );
  }
}

// Check if function exists and hook into setup.
if ( function_exists( 'acf_register_block_type' ) ) {
  add_action( 'acf/init', 'theme_register_blocks' );
}

page-section块的模板代码:

<div class="container">
  <InnerBlocks />
</div>

general块的模板代码:

<?php
/**
 * ACF General Block template.
 *
 * @param array $block The block settings and attributes
 * @param string $content Inner blocks content if jsx is enabled
 * @param boolean $is_preview True during preview render
 * @param array $context any context variables from parent blocks if applicable
 */

$fields = get_fields(); ?>

<div <?php block_id($block); ?>class="container<?php echo $fields['show_sidebar'] ? ' grid' : '';">

  <div class="cell col-lg-8">
    <?php 
      // Only add H2 HTML if there is a heading
      if( !empty($fields['heading']) ): ?>
        <h2><?php echo $fields['heading']; ?></h2><?php 
      endif; 

      echo $fields['content']; 
    ?>
  </div>
  <?php if ( $fields['show_sidebar'] ): ?>
    <div class="col-lg-4">
      <?php echo $fields['sidebar']['type']; ?>
    </div>
  <?php endif; ?>

</div>

请问我哪里配置出错了?


解决方案

问题出在你没有在块的supports配置里明确开启inner_blocks支持,仅设置jsx => true不足以让编辑器允许插入内部块。ACF 5.12+版本开始支持内部块功能,需要同时配置两个参数:

  1. 在supports数组中添加'inner_blocks' => true
  2. 保持'jsx' => true以渲染<InnerBlocks />组件

修改后的page-section块配置如下:

$blocks[] = [
    'name' => 'page-section',
    'title' => 'Page Section',
    'mode' => 'edit',
    'supports' => [
        'jsx' => true,
        'mode' => false,
        'align' => false,
        'anchor' => true,
        'inner_blocks' => true // 新增这一行
    ]
];

额外注意事项:

  • 确认你的ACF版本在5.12以上,旧版本不支持inner_blocks参数
  • 模板中的<InnerBlocks />标签要保持驼峰命名,不能写错
  • 修改后清空浏览器缓存和WordPress的块缓存,刷新编辑器页面测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 05:28:17