本記事では、WordPressのthe_excerpt関数で出力されるpタグを削除する方法について解説しています。この方法を使うことで、pタグではなくその他のタグでテキストを囲うことや特定のクラスを付与したpタグに変更することも可能です。
WordPressの学習におすすめ書籍
1冊ですべて身につくWordPress入門講座

メリット
- サイト運営に必須のことが学べる
- 基本的な使い方~オリジナルテーマ開発まで学べる


Amazon Kindle Unlimitedに登録すると、月額980円で読み放題だからオススメだよ!



初回30日間は無料だから、まだ登録したことのない人はぜひ試してみてね!
全プログラマー必見!
変数名/関数名にもう悩まない!
変数名/関数名にもう悩まない!
リーダブルコード


メリット
- 美しいコードが書けるが自然と書けるようになる
- 他の開発者が理解しやすいコードになる
著:Dustin Boswell, 著:Trevor Foucher, 解説:須藤 功平, 翻訳:角 征典
¥2,640 (2023/07/23 02:48時点 | Amazon調べ)


目次
get_the_excerpt関数
the_excerpt関数では、デフォルトでpタグに囲われた抜粋テキストを表示するため、代わりにget_the_excerpt関数を使用します。
<?php $excerpt = get_the_excerpt(); ?>
基本的な使い方はthe_excerpt関数と同じで、異なる点は抜粋テキストを表示ではなく取得します。そのため、取得したテキストをechoで表示させる必要があります。
<?php echo get_the_excerpt(); ?>