
內容簡介
這個外掛可以讓你解析所有類型的 Gutenberg 區塊,並在你自己的模板中呈現它們。
使用方法
在你的佈景主題中使用過濾器 ‘jinx_block_renderers’ 來呈現 Gutenberg 區塊。
<?php
function my_render_function(array $fields)
{
echo $fields['url'];
}
add_filter('jinx_block_renderers', function($renderers) {
$renderers[] = [
'block' => 'core-embed/youtube', // 區塊的名稱
'conditions' => [], // 預設值 - 接受一個鍵值對的陣列
// 'conditions' => [
// 'className' => 'is-style-custom', // 字串會用在正規表示式中
// 'field' => function($value) { // 回呼函數會接收值並必須回傳 true 或 false
// return $value === 'some value';
// }
// ],
'fields' => [
'url' => [ // 你的參數名稱
'path' => '//iframe[@src]',
'item' => 0,
// 'item' => 9, // 如果不存在,會回傳 null
// 'item': 'first', // 常數 Jinx\BlockRenderer\Field::ITEM_FIRST
// 'item': 'last', // 常數 Jinx\BlockRenderer\Field::ITEM_LAST
// 'item': null // 預設值 - 將所有結果作為陣列回傳
'attr' => 'src',
//'attr' => null // 預設值 - 回傳結果的值
//'filter' => 'my_filter_function' // 對每個匹配到的欄位使用過濾器
],
],
// 'insert' => '//*[contains(@class,"wp-block-")]' // 預設值 - 將呈現的區塊插入回區塊包裝器中
'template' => __DIR__.'/youtube-video.php', // 相對路徑,模板會將每個欄位視作變數
// 'callback' => 'my_render_function'
];
return $renderers;
});
?>
外掛標籤
開發者團隊
原文外掛簡介
The plugin allows you to parse all kinds gutenberg blocks and render them in your own template.
Usage
Use the filter ‘jinx_block_renderers’ in your theme to render gutenberg blocks.
'core-embed/youtube', // name of the block
'conditions' => [], // default - accepts an array of key value pairs
// 'conditions' => [
// 'className' => 'is-style-custom', // strings are used inside a regular expression
// 'field' => function($value) { // callbacks will receive the fields value and must return true or false
// return $value === 'some value';
// }
// ],
'fields' => [
'url' => [ // name of your parameter
'path' => '//iframe[@src]',
'item' => 0,
// 'item' => 9, // if not exists, will return null
// 'item': 'first', // constant Jinx\BlockRenderer\Field::ITEM_FIRST
// 'item': 'last', // constant Jinx\BlockRenderer\Field::ITEM_LAST
// 'item': null // default - returns all results as an array
'attr' => 'src',
//'attr' => null // default - returns the results value
//'filter' => 'my_filter_function' // filters each matched field
],
],
// 'insert' => '//*[contains(@class,"wp-block-")]' // default - inserts rendered block back into the block-wrapper
'template' => __DIR__.'/youtube-video.php', // relative path, templates will handle each field as a variable
// 'callback' => 'my_render_function'
];
return $renderers;
});
?>
