説明
get_the_excerpt() は、投稿の抜粋を文字列として返す関数です。
基本構文
get_the_excerpt( int|WP_Post $post = null ): string
引数
$post:投稿IDまたはオブジェクト
戻り値
抜粋の文字列。
使い方(サンプル)
echo esc_html( get_the_excerpt() );
注意点
- 抜粋が空のときは自動生成された値が返ります。
- 長さの調整は
excerpt_lengthフィルターで行います。
実務での使いどころ
- 投稿の抜粋を、文字列として受け取って使うとき。
- 一覧のカードや、
meta descriptionに抜粋を使うとき。
よくあるミスと対処
- 抜粋が未入力でも、本文から自動で生成される。意図した文章にしたいときは、抜粋欄に入力する。
- 自動生成の長さは、
excerpt_lengthフィルターで変更する。 - ループの外で、投稿を指定せずに呼んでいる。
NG例
echo get_the_excerpt(); // ループの外では、意図した投稿の抜粋にならない
OK例
echo esc_html( get_the_excerpt( $post_id ) );
使い分け早見表
| やりたいこと | 使うもの |
|---|---|
| 抜粋を、そのまま出力 | the_excerpt() |
| 抜粋を、文字列として取得 | get_the_excerpt() |
| 本文を取得 | get_the_content() |
関連項目
公式リファレンス:get_the_excerpt() | Function | WordPress Developer Resources

コメント