尝试为 Gutenberg 创建 ACF 块时出现“不存在块类型”
Posted
技术标签:
【中文标题】尝试为 Gutenberg 创建 ACF 块时出现“不存在块类型”【英文标题】:"No block type exists" when trying to create ACF block for Gutenberg 【发布时间】:2021-04-16 11:37:54 【问题描述】:我正在尝试初始化一个名为hero
的新块。目前,我正在尝试为该块分配一个 ACF 字段组,但目前在 WordPress 后端看到了这个:
我已经创建了块并注册了块类型,但看不到哪里有问题?
这是我的方法:
/acf-blocks/blocks.php
<?php
function register_acf_block_types()
$hero = array(
'name' => 'hero',
'title' => __('Hero'),
'description' => __(''),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array( 'hero' ),
);
$blocks = [
$hero
];
return $blocks;
// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') )
add_action('acf/init', 'register_acf_block_types');
?>
/acf-blocks/render.php
<?php
/**
* This is the callback that renders the block.
*/
function block_render( $block )
// convert name ("acf/testimonial") into path friendly slug ("testimonial")
$slug = str_replace('acf/', '', $block['name']);
// include a template part from within the "template-parts/block" folder
if( file_exists( get_theme_file_path("/template-parts/block/content-$slug.php") ) )
include( get_theme_file_path("/template-parts/block/content-$slug.php") );
?>
/acf-blocks/functions.php
<?php
function block_acf_init()
$blocks = require(__DIR__.'/inc/acf-blocks/blocks.php');
foreach($blocks as $block)
acf_register_block($block);
?>
/template-parts/block/hero.php
<?php
/*
* Block Name: hero
*/
?>
这是我的文件夹结构:
theme
inc
acf-blocks
blocks.php
functions.php
render.php
template-parts
blocks
hero.php
【问题讨论】:
【参考方案1】:将 foreach 循环中的 ACF 函数(在 /acf-blocks/functions.php 中)更改为:
foreach($blocks as $block)
acf_register_block_type($block);
【讨论】:
以上是关于尝试为 Gutenberg 创建 ACF 块时出现“不存在块类型”的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 wordpress javascript 中提取 acf 字段时出现错误