【发布时间】:2021-04-23 22:11:44
【问题描述】:
我正在创建自定义 ACF Gutenberg 阻止我的网站,并成功地注册了我自己的阻止。现在,我有一个名为Blog 的自定义帖子类型。我不希望blog 显示我所有的 ACF Gutenberg 块,我想创建一个单独的批处理,仅供自定义帖子类型使用。我启用了show_in_rest,但即使是默认的 Gutenberg 博客也不显示?
这是我的方法:
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;
?>
- 注册所有区块(theme/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 块显示在页面一侧,当我只希望它用于自定义帖子类型时:
【问题讨论】:
标签: php wordpress custom-post-type wordpress-gutenberg acf-gutenberg