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

创建Gutenberg ACF块时出现命名空间前缀错误求助

解决Gutenberg ACF块命名空间前缀错误

核心问题原因

错误提示Notice: WP_Block_Type_Registry::register was called incorrectly. Block type names must contain a namespace prefix.的本质是你的block.json文件中name字段格式不符合要求,WordPress要求块名称必须是命名空间/块标识的格式(比如my-theme/testimonial),不能是单纯的testimonial。

步骤1:修正block.json的name字段

找到blocks/testimonial/block.json文件,确保name字段包含命名空间前缀,示例:

{
  "name": "my-theme/testimonial",
  "title": "Testimonial Block",
  "description": "自定义ACF testimonial块",
  "category": "formatting",
  "icon": "admin-comments",
  "keywords": ["testimonial", "quote"],
  "acf": {
    "mode": "preview",
    "renderTemplate": "template.php"
  },
  "supports": {
    "align": true
  }
}

命名空间可以用你的主题/插件标识(比如主题slug),保证唯一性即可

步骤2:修正functions.php中的注册路径

之前的代码错误使用了get_template_directory_uri()(这是前端URL路径,不是服务器本地文件路径),正确写法如下:

add_action( 'init', 'register_acf_blocks' );
function register_acf_blocks() {
    // 使用get_template_directory()获取主题根目录的服务器本地路径
    register_block_type( get_template_directory() . '/blocks/testimonial' );
}

如果__DIR__指向的是functions.php所在的主题根目录,用它替代get_template_directory()也可以。

额外验证点

  • 确认block.json里acf.renderTemplate指向的模板文件路径正确(路径是相对于block.json所在目录)
  • 清除WordPress缓存和浏览器缓存,确保新配置生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 16:32:17