block_core_latest_posts_get_excerpt_length()


WordPressのblock_core_latest_posts_get_excerpt_length()関数は、最新の投稿ブロックで使用される抜粋の長さを取得するための関数です。

構文

block_core_latest_posts_get_excerpt_length( int $excerpt_length );
  • $excerpt_length(int)—抜粋の文字数を指定します。デフォルトは55文字です。

例1:デフォルトの抜粋長を取得

最新投稿ブロックのデフォルトの抜粋長を取得します。

$excerpt_length = block_core_latest_posts_get_excerpt_length( 55 );

例2:カスタム抜粋長を設定

抜粋の長さを100文字に変更します。

add_filter( 'excerpt_length', function() { return block_core_latest_posts_get_excerpt_length( 100 ); } );

例3:フックを使用した抜粋長の変更

フックを使って抜粋の長さを動的に変更します。

add_filter( 'block_core_latest_posts_excerpt_length', function( $length ) { return 30; } );

例4:特定の条件で抜粋長を変更

カテゴリが「news」の場合、抜粋長を短くします。

if ( has_category( 'news' ) ) { $excerpt_length = block_core_latest_posts_get_excerpt_length( 20 ); }

例5:抜粋なしで表示

抜粋を完全に非表示にします。

$excerpt_length = block_core_latest_posts_get_excerpt_length( 0 );

例6:テーマ内での使用

テーマのfunctions.phpで抜粋長をグローバルに設定します。

add_action( 'after_setup_theme', function() { block_core_latest_posts_get_excerpt_length( 40 ); } );

例7:ショートコード内での利用

ショートコードで最新投稿の抜粋長を制御します。

add_shortcode( 'custom_excerpt', function() { return block_core_latest_posts_get_excerpt_length( 25 ); } );

注意点

  • この関数はブロックエディタの最新投稿ブロック専用です。
  • 値を極端に大きくするとレイアウトが崩れる可能性があります。
  • フックを使用する場合は優先順位に注意が必要です。

関連機能: