我正在尝试使用 ACF 和 Wordpress 为 Gutenberg 创建一个自定义“列”块,所以我想要一个易于管理的列块,用户可以在其中输入一些代表所需列数量的数字(例如,5)和a for 将创建该列。
一切正常,因为我尝试创建多个内部块......我的代码以这种方式开始:
acf_register_block_type([
'name' => "theme-columns",
'title' => __("Columns", "mytheme"),
'description' => __("Columns", "mytheme"),
'category' => "theme-blocks",
'icon' => "grid-view",
'render_callback' => [$this->renderers, "columns_renderer"],
'mode' => "preview",
'supports' => [
'align' => true,
'mode' => false,
'jsx' => true
],
'enqueue_assets' => function(){
wp_enqueue_style("bootstrap");
}
]);
和columns_renderer
功能:
public function columns_renderer()
{
$count = get_field("columns-count");
?>
<div class="row row-cols-1 row-cols-md-3 row-cols-xl-5 justify-content-center">
<?php for($i = 0; $i < $count; $i++): ?>
<div class="col">
<InnerBlocks />
</div>
<?php endfor; ?>
</div>
<?php
}
所以,正如(不是)预期的那样,它不起作用,因为古腾堡不支持<InnerBlocks />
每个块多个......在网上搜索,我发现有些人谈论这样做就像core/column
块一样,使用一些“黑客”......但我不知道该怎么办...
有人可以帮助我并给我一些方法来达到我的需要吗?
谢谢!
- 更新 -
试图创建一个“列”块并将“列”设置为仅接受新创建的“列”块,但仍然无法正常工作......
public function column()
{
$this->register_block([
'name' => "theme-column",
'title' => __("Column", "mytheme"),
'description' => __("Column", "mytheme"),
'category' => "theme-blocks",
'icon' => "columns",
'render_callback' => [$this->renderers, "column_renderer"],
'mode' => "preview",
'supports' => [
'align' => true,
'mode' => false,
'jsx' => true
],
'enqueue_assets' => function(){
wp_enqueue_style("theme-main");
}
]);
}
public function column_renderer()
{
?>
<InnerBlocks />
<?php
}
public function columns()
{
$this->register_block([
'name' => "theme-columns",
'title' => __("Columns", "mytheme"),
'description' => __("Columns", "mytheme"),
'category' => "theme-blocks",
'icon' => "columns",
'render_callback' => [$this->renderers, "columns_renderer"],
'mode' => "preview",
'supports' => [
'align' => true,
'mode' => false,
'jsx' => true
],
'enqueue_assets' => function(){
wp_enqueue_style("theme-main");
}
]);
}
public function columns_renderer()
{
$allowedBlocks = ["acf/theme-column"];
$template = array(
array('acf/biore-column', []),
);
$column_count = get_field("columns-count");
?>
<div class="row py-4">
<?php for($i = 0; $i < $column_count; $i++): ?>
<div class="col">
<InnerBlocks allowedBlocks="<?= esc_attr(wp_json_encode($allowedBlocks)); ?>" template="<?= esc_attr(wp_json_encode($template)); ?>" />
</div>
<?php endfor; ?>
</div>
<?php
}