register_block_core_post_featured_image()


WordPressのregister_block_core_post_featured_image()関数は、投稿のアイキャッチ画像を表示するためのブロックを登録するために使用されます。この関数は、ブロックエディタ内でアイキャッチ画像を簡単に表示するための機能を提供します。

構文

register_block_core_post_featured_image( array $args );

引数の説明:

  • $args(array) — ブロックの設定を定義するための配列。以下のキーを含むことができます:
    • 'name'(string) — ブロックの名前。
    • 'title'(string) — ブロックのタイトル。
    • 'description'(string) — ブロックの説明。
    • 'render_callback'(callable) — ブロックのレンダリングを行うコールバック関数。

例1: 基本的なブロックの登録

この例では、アイキャッチ画像を表示するための基本的なブロックを登録します。

register_block_core_post_featured_image( array( 'name' => 'core/post-featured-image', 'title' => 'アイキャッチ画像', 'description' => '投稿のアイキャッチ画像を表示します。' ) );

例2: カスタムレンダリング関数の使用

カスタムレンダリング関数を使用して、アイキャッチ画像を表示するブロックを登録します。

function render_custom_featured_image() { return get_the_post_thumbnail(); } register_block_core_post_featured_image( array( 'name' => 'core/custom-featured-image', 'render_callback' => 'render_custom_featured_image' ) );

例3: ブロックのタイトルを変更

ブロックのタイトルをカスタマイズして登録します。

register_block_core_post_featured_image( array( 'name' => 'core/post-featured-image', 'title' => 'カスタムアイキャッチ画像' ) );

例4: ブロックの説明を追加

ブロックの説明を追加して登録します。

register_block_core_post_featured_image( array( 'name' => 'core/post-featured-image', 'description' => 'このブロックは投稿のアイキャッチ画像を表示します。' ) );

例5: 複数のブロックを一度に登録

複数のブロックを一度に登録する例です。

register_block_core_post_featured_image( array( 'name' => 'core/post-featured-image', 'title' => 'アイキャッチ画像1' ) ); register_block_core_post_featured_image( array( 'name' => 'core/post-featured-image2', 'title' => 'アイキャッチ画像2' ) );

例6: ブロックのレンダリング関数に引数を渡す

レンダリング関数に引数を渡して、アイキャッチ画像のサイズを指定します。

function render_featured_image_with_size( $attributes ) { return get_the_post_thumbnail( null, $attributes['size'] ); } register_block_core_post_featured_image( array( 'name' => 'core/post-featured-image', 'render_callback' => 'render_featured_image_with_size' ) );

例7: ブロックの属性を追加

ブロックにカスタム属性を追加して登録します。

register_block_core_post_featured_image( array( 'name' => 'core/post-featured-image', 'attributes' => array( 'size' => array( 'type' => 'string', 'default' => 'thumbnail' ) ) ) );

注意点

  • この関数は、ブロックエディタ内でのみ使用されます。
  • レンダリング関数は、適切なHTMLを返す必要があります。
  • ブロックの名前は一意である必要があります。

関連機能: