説明
get_the_content() は、投稿の本文を文字列として返す関数です。
基本構文
get_the_content( string $more_link_text = null, bool $strip_teaser = false, int|WP_Post $post = null ): string
引数
$more_link_text:続きを読むのテキスト$strip_teaser:moreより前を除くか$post:投稿IDまたはオブジェクト
戻り値
本文の文字列。
使い方(サンプル)
$content = apply_filters( 'the_content', get_the_content() );
注意点
- 整形やショートコードの展開は行われません。表示する場合は
apply_filters( 'the_content', ... )を通します。
実務での使いどころ
- 投稿の本文を、文字列として受け取って加工するとき(文字数の制限、タグの除去など)。
- 出力せずに、変数に入れて使いたいとき。
よくあるミスと対処
the_contentフィルターが適用されない。ショートコードが展開されず、自動の段落(wpautop)も付かない。- そのまま
echoして、本文が崩れる。表示するときはapply_filters( 'the_content', get_the_content() )を通す。 - ループの外で、投稿を指定せずに呼んでいる。
NG例
echo get_the_content(); // ショートコードが展開されない
OK例
echo apply_filters( 'the_content', get_the_content() );
使い分け早見表
| やりたいこと | 使うもの |
|---|---|
| フィルター済みの本文をそのまま出力 | the_content() |
| 本文を、加工前の文字列として取得 | get_the_content() |
| 抜粋を取得 | get_the_excerpt() |
関連項目
the_content()、get_the_excerpt()
公式リファレンス:get_the_content() | Function | WordPress Developer Resources

コメント