register_block_core_latest_posts()


WordPressのregister_block_core_latest_posts()関数は、「最新の投稿」ブロックを登録するために使用されます。この関数はブロックエディタで利用可能なブロックを追加します。

構文

register_block_core_latest_posts( array $args = array() );
  • $args (array) — ブロックの属性、サポートオプション、レンダリングコールバックなどを設定する配列

例1: 基本的な使用法

最新の投稿ブロックを登録する最もシンプルな例。

add_action( 'init', function() { register_block_core_latest_posts(); } );

例2: カスタム属性を設定

ブロックにカスタム属性を追加する例。

add_action( 'init', function() { register_block_core_latest_posts( array( 'attributes' => array( 'postsToShow' => array( 'type' => 'number', 'default' => 5 ) ) ) ); } );

例3: カスタムレンダリングコールバック

ブロックの出力をカスタマイズするコールバック関数を指定。

add_action( 'init', function() { register_block_core_latest_posts( array( 'render_callback' => 'my_custom_render' ) ); } );

例4: ブロックサポートの追加

ブロックにカスタムサポートオプションを追加。

add_action( 'init', function() { register_block_core_latest_posts( array( 'supports' => array( 'align' => true ) ) ); } );

例5: カテゴリ制限付きで登録

特定のカテゴリの投稿のみ表示するように設定。

add_action( 'init', function() { register_block_core_latest_posts( array( 'attributes' => array( 'categories' => array( 'type' => 'string' ) ) ) ); } );

例6: 並べ替えオプションの追加

投稿の並べ替え方法をカスタマイズ。

add_action( 'init', function() { register_block_core_latest_posts( array( 'attributes' => array( 'orderBy' => array( 'type' => 'string', 'default' => 'date' ) ) ); } );

例7: カスタムCSSクラスの追加

ブロックに追加のCSSクラスを指定。

add_action( 'init', function() { register_block_core_latest_posts( array( 'attributes' => array( 'className' => array( 'type' => 'string' ) ) ); } );

注意点

  • この関数はWordPress 5.0以降で利用可能
  • ブロックエディタが有効である必要があります
  • カスタムレンダリングコールバックを使用する場合は適切なHTMLを返す必要があります

関連機能: