我正在创建自定义 ACF Gutenberg 阻止我的网站,并成功地注册了我自己的块。现在,我有一个名为Blog
. 我不想blog
显示我所有的 ACF Gutenberg 块,我想创建一个单独的批次,仅供自定义帖子类型使用。我已启用show_in_rest
,但即使是默认的古腾堡博客也不显示给我?
这是我的方法:
1.注册帖子类型(theme/functions.php)
<?php
register_post_type('Blog', theme_build_post_args('Blog', 'Blog', 'Blog', array(
'show_in_rest' => true,
'menu_icon' => 'dashicons-edit',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array(
'editor',
'title',
'author',
'revisions',
'excerpt',
'thumbnail'
) ,
)));
?>
2. 为页面注册 ACF Gutenberg 块(theme/inc/acf-blocks/blocks.php)
以下是我注册用于页面(而不是blog
帖子类型)的块:
<?php
$hero = array(
'name' => 'hero',
'title' => __('Hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
) ,
);
$blocks = [$hero];
return $blocks;
?>
- 为博客文章类型注册 ACF Gutenberg 块 ( theme/inc/acf-blocks/blog-blocks.php )
<?php
$blog_hero = array(
'name' => 'blog_hero',
'title' => __('Blog hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
) ,
);
$blog_blocks = [$blog_hero];
return $blog_blocks;
?>
- 注册所有块(主题/inc/acf-blocks/functions.php)
<?php
/*
* loop though array and register each block type
*/
function block_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blocks.php';
$blocks = require($path);
foreach($blocks as $block) {
acf_register_block_type($block);
}
}
function blog_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blog-blocks.php';
$blog_blocks = require($path);
foreach($blog_blocks as $blog_block) {
acf_register_block_type($blog_block);
}
}
// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') ) {
add_action('acf/init', 'block_acf_init');
add_action('acf/init', 'blog_acf_init');
}
?>
当前结果:
在自定义帖子类型上创建帖子时blog
,我无法添加任何块,更不用说查看是否blog_hero
出现块:
在页面上,我可以看到我创建的所有块,但是,blog hero
当我只希望它用于自定义帖子类型时,该块显示在页面一侧: