WordPressのthe_excerpt()をカスタマイズする
WordPressのthe_excerpt()は、抜粋を表示するためのテンプレートタグです。ここではTwenty Elevenテーマを使って、the_excerpt()の末尾に表示する内容を変更する方法を解説します。
Twenty Elevenテーマでは、検索結果の本文の一部を表示するためにthe_excerpt()を使っており、抜粋の最後に「続きを読む→」を表示しています。
デフォルトの検索結果
以下、the_excerpt()に掲載されているサンプルを拝借して、カスタマイズ方法を紹介します。それぞれのコードは利用テーマのfunctions.phpに追加してください。
1.末尾の文字列を変更する
function new_excerpt_more($more) {
return ' ..... ';
}
add_filter('excerpt_more', 'new_excerpt_more');
2.末尾に何も出力しないようにする
function new_excerpt_more($more) {
return '';
}
add_filter('excerpt_more', 'new_excerpt_more');
3.「続きを読む」のパーマリンクを設定する
function new_excerpt_more($post) {
return ' <a href="'. esc_url( get_permalink() ) . '">' . 'Read the Rest...' . '</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
4.Twenty Elevenテーマの場合
冒頭のTwenty Elevenテーマの「続きを読む→」という表示は、functions.phpに次のようなexcerpt_moreフィルタが設定することで実現しています。
function twentyeleven_continue_reading_link() {
return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continue reading <span class="meta-nav">→</span>', 'twentyeleven' ) . '</a>';
}
function twentyeleven_auto_excerpt_more( $more ) {
return ' …' . twentyeleven_continue_reading_link();
}
add_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );
5.excerpt_moreフィルタについて
excerpt_moreフィルタは、wp-includes/formatting.phpのwp_trim_excerpt()に実装されています。
function wp_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
excerpt_moreフィルタでwp_trim_excerpt()を起動する定義は、default-filters.phpにあります。
add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
6.抜粋の文字数を変更する
本エントリーとは主旨が異なりますが、抜粋に表示する文字数を変更する場合は「WordPressで抜粋の文字数を変更する方法」を参照してください。
英語の場合は次のコードで実現できます。
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
WordPressで抜粋の文字数を変更する方法
WordPressで抜粋の文字数を変更する方法を紹介します。WordPressは3.2.1を使っています。
1.問題点
下のスクリーンショットはTwenty Elevenテーマを使った検索結果ページです。検索結果ページではthe_excerpt()を使って(本文の)抜粋を表示するようにしていますが、日本語の場合、the_excerpt()を使っても全文が表示されてしまいます。
理由は、以下の公式ドキュメントによると、「語句の間を半角スペースで区切らない言語では判定できない」ということらしいです。
2.対策
WordPressを使っている方はすでにご存知と思いますが、同梱されている「WP Multibyte Patch」プラグインを有効化します。
これで110文字を抜粋して表示するようになります。
3.文字数を変更する
2項の設定で110文字になるのは「WP Multibyte Patch」プラグインのデフォルト設定によるものです。
文字数を変更する場合は、さらに以下の手順を踏んでください。
まず、wp-content/plugins/wp-multibyte-patch/wpmp-config-sample.phpをコピーしてwpmp-config.phpを作成します。
次に作成したwpmp-config.phpを任意のエディタで開き、下記の「110」の部分を修正します。
$wpmp_conf['excerpt_mblength'] = 110;
修正したwpmp-config.phpをwp-content配下にアップロードします。
例えば「75」に変更すると、次のようにさらに抜粋を短く表示することができます。
ということで、抜粋に任意の文字数を表示できるようになります。お試しください。