2026年11月9日
2026年11月9日
WordPressの抜粋(excerpt)をカスタマイズする方法・文字数・続きを読む変更
はじめに
「記事一覧の抜粋が短すぎる・長すぎる」「『続きを読む』のテキストを変更したい」——WordPressの抜粋(excerpt)はフィルターフックで簡単にカスタマイズできます。表示文字数から読み込みスタイルまで解説します。
症状・原因
WordPressのデフォルト抜粋は55語(英語基準)で切り取られます。日本語では文字数の扱いが異なり、長くなりすぎることがあります。また「[…]」という末尾の省略記号も変更できます。
解決手順
ステップ1:抜粋の文字数を変更する
// 抜粋の文字数を変更(デフォルト55語)
add_filter( 'excerpt_length', function( $length ) {
return 120; // 120文字に変更(日本語向け)
} );
// 投稿タイプ別に抜粋文字数を変更
add_filter( 'excerpt_length', function( $length ) {
if ( is_post_type_archive( 'product' ) ) {
return 80; // 商品一覧は短め
}
if ( is_home() ) {
return 150; // ブログトップは長め
}
return $length;
} );
ステップ2:省略記号と「続きを読む」テキストを変更する
// 「[…]」を「…」に変更
add_filter( 'excerpt_more', function() {
return '…';
} );
// 「続きを読む」リンクを追加
add_filter( 'excerpt_more', function() {
return '… <a class="read-more" href="' . get_permalink() . '">続きを読む</a>';
} );
// get_the_excerpt()でも同様に適用される
// テンプレートで使う場合:
// echo get_the_excerpt();
// the_excerpt();
ステップ3:カスタム抜粋生成関数を作る
// 日本語対応のカスタム抜粋関数
function get_custom_excerpt( $post_id = null, $length = 120, $more = '…' ) {
$post = get_post( $post_id );
if ( ! $post ) return '';
// 手動抜粋が設定されていればそれを使用
if ( $post->post_excerpt ) {
return wp_strip_all_tags( $post->post_excerpt );
}
// 本文から自動生成
$content = wp_strip_all_tags(
strip_shortcodes( $post->post_content )
);
// 日本語対応の文字数カウント(mb_strimwidth)
if ( mb_strlen( $content ) > $length ) {
return mb_strimwidth( $content, 0, $length, $more );
}
return $content;
}
// 使い方
// echo get_custom_excerpt( get_the_ID(), 100, '…続きを読む' );
ステップ4:抜粋にHTMLタグを残す
// デフォルトの excerpt はHTMLをすべて除去する
// 一部のHTMLタグを保持する抜粋フィルター
add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
if ( $post->post_excerpt ) {
// 手動抜粋は<strong><em><a>タグを許可
return wp_kses( $post->post_excerpt, [
'strong' => [],
'em' => [],
'a' => [ 'href' => [] ],
] );
}
return $excerpt;
}, 10, 2 );
// ショートコードを抜粋から除去
add_filter( 'get_the_excerpt', function( $excerpt ) {
return strip_shortcodes( $excerpt );
} );
ステップ5:投稿一覧でサムネイル付き抜粋を表示する
// 投稿一覧用カスタムループテンプレート
function render_post_card( $post ) {
$excerpt = get_custom_excerpt( $post->ID, 100 );
$thumb_url = get_the_post_thumbnail_url( $post->ID, 'medium' );
$date = get_the_date( 'Y年m月d日', $post );
$url = get_permalink( $post );
?>
<article class="post-card">
<?php if ( $thumb_url ) : ?>
<a href="<?php echo esc_url( $url ); ?>">
<img src="<?php echo esc_url( $thumb_url ); ?>"
alt="<?php echo esc_attr( get_the_title( $post ) ); ?>"
loading="lazy">
</a>
<?php endif; ?>
<div class="post-card__body">
<time datetime="<?php echo get_the_date( 'c', $post ); ?>">
<?php echo esc_html( $date ); ?>
</time>
<h2><a href="<?php echo esc_url( $url ); ?>">
<?php echo esc_html( get_the_title( $post ) ); ?>
</a></h2>
<p class="excerpt"><?php echo esc_html( $excerpt ); ?></p>
<a href="<?php echo esc_url( $url ); ?>" class="read-more">
続きを読む →
</a>
</div>
</article>
<?php
}
注意事項
excerpt_lengthフィルターの単位は「語数(words)」ですが、日本語の場合は文字数として扱われます。日本語サイトではmb_strimwidth()を使ったカスタム関数の方が正確に制御できます。- 手動抜粋(投稿編集画面の抜粋フィールド)は
excerpt_lengthフィルターの影響を受けません。手動抜粋は設定した内容がそのまま表示されます。 - 抜粋をOGP(og:description)にも使用する場合は、特殊文字のエスケープと改行の除去が必要です。
esc_attr( wp_strip_all_tags( get_the_excerpt() ) )で安全に出力してください。
まとめ
抜粋のカスタマイズは「excerpt_lengthで文字数変更→excerpt_moreで省略記号変更→mb_strimwidthで日本語対応関数作成→投稿カードテンプレートに組み込み」の流れで実装できます。手動抜粋と自動抜粋を使い分けることで柔軟なコンテンツ管理が実現します。関連記事:WordPressのナビゲーションメニューをカスタマイズする方法、WordPressでカスタムループを実装する方法。