Skip to content

Instantly share code, notes, and snippets.

@Olein-jp
Created April 29, 2024 06:58
Show Gist options
  • Save Olein-jp/20442aef7499f2adba30da45e4085bb9 to your computer and use it in GitHub Desktop.
Save Olein-jp/20442aef7499f2adba30da45e4085bb9 to your computer and use it in GitHub Desktop.
ブロックバリエーションを作成してカスタムフィールドを出力するサンプル
function book_introduce_block_type_variations( $variations, $block_type ) {
if ( 'core/heading' === $block_type->name ) {
$variations[] = array(
'name' => 'book-title',
'title' => 'Book Title',
'attributes' => array(
'metadata' => array(
'bindings' => array(
'content' => array(
'source' => 'acf/field',
'args' => array(
'key' => 'book-title',
),
),
),
),
),
);
} elseif ( 'core/image' === $block_type->name ) {
$variations[] = array(
'name' => 'book-thumbnail',
'title' => 'Book Thumbnail',
'attributes' => array(
'metadata' => array(
'bindings' => array(
'url' => array(
'source' => 'acf/field',
'args' => array(
'key' => 'book-thumbnail',
),
),
),
),
),
);
} elseif ( 'core/button' === $block_type->name ) {
$variations[] = array(
'name' => 'book-amazon-url',
'title' => 'Book Amazon URL',
'attributes' => array(
'metadata' => array(
'bindings' => array(
'url' => array(
'source' => 'acf/field',
'args' => array(
'key' => 'book-amazon-url',
),
),
),
),
),
);
}
return $variations;
}
add_filter( 'get_block_type_variations', 'book_introduce_block_type_variations', 10, 2 );
@Olein-jp
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment