php - ServerSideRender 属性不传递给后端
问题描述
我正在使用 @wordpress/server-side-render 从后端获取 Gutenberg 块的内容。
if ( props && props.attributes ) {
blockContent = <ServerSideRender
block="test/employee-block"
attributes={ props.attributes }
/>
}
这是块的PHP端
register_block_type( 'test/employee-block', array(
'title' => 'Test Block',
'description' => 'test',
'category' => 'widgets',
'icon' => 'admin-users',
'api_version' => 2,
'editor_script' => 'employees_dynamic_block_script',
'render_callback' => function ($block_attributes, $content) {
return '<h1>test</h1>';
}
) );
但是,渲染回调函数的 $block_attributes 始终为空。我不知道为什么,但根据API 文档,它应该在那里。
解决方案
找到了解决方法,如果您没有在 register_block_type 函数中定义参数键和类型,它不会将它们传递给渲染回调。
register_block_type( 'test/employee-block', array(
'api_version' => 2,
'editor_script' => 'employees_dynamic_block_script',
'attributes' => array(
'selectControl' => array(
'type' => 'string',
'default' => '0',
)
),
'render_callback' => function ($block_attributes, $content) {
return '<h1>test</h1>';
}
) );
推荐阅读
- pine-script - Pinescript TradingView 回测策略不产生任何数据
- javascript - 寻找类似 ParentNode.append 但替换 ParentNode 的内容而不是添加到它的方法
- python - 从 Python 的子进程中捕获数据
- python - Discord.py 循环数据到文本文件?
- r - R, 两边合闸
- javascript - 有没有办法让两个数字整除并打印更大的数字
- php - 如何将这些查询与另一个父表中的 where 子句组合成单个查询?
- c# - 即使我添加了测试单元,sonarqube 的覆盖率也始终为 0%
- unit-testing - 在 tests/test.rs 文件中无法识别 src/main.rs 中定义的类型
- javascript - 从递延函数 for() 计数