- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「テンプレートタグ/the excerpt」の版間の差分
(→投稿で "read more" リンクを作成する: 追加) |
(最新版に表記を合わせました。v1.5以前のコードはコメントアウトしました。) |
||
3行目: | 3行目: | ||
</div> | </div> | ||
− | このテンプレートタグ <code>'''the_excerpt()'''</code> は、現在の投稿の[[用語集#Excerpt|抜粋]]を、文末に [...] | + | このテンプレートタグ <code>'''the_excerpt()'''</code> は、現在の投稿の[[用語集#Excerpt|抜粋]]を、文末に ''[...]''(角括弧+三点リーダー)をつけて表示します。この三点リーダーは「続きを読む」リンクではありません。抜粋を直接表示するために使われますが、[[関数リファレンス/get the excerpt|<tt>get_the_excerpt()</tt>]] から返された文字列に開始と終了の <tt><p></tt> タグを追加します。[[Write Post SubPanel|投稿作成/編集画面]] の抜粋文入力欄に記入してあればそれを、抜粋を入力していない場合は投稿内容の最初の 55個の単語までを表示します('''注''')。また、抜粋のコンテンツからは [[用語集#HTML|HTML]] タグと画像は取り除かれます。このタグは[[The Loop|ループ]]内でのみ使えます。 |
− | 現在の投稿が attachment.php および image.php テンプレートループ内の [[Using Image and File Attachments|attachment]]/[[:en:Using Image and File Attachments|en]] の場合、attachment のキャプションを表示します。キャプションには、抜粋の [...] 記号は含まれません。 | + | 現在の投稿が ''attachment.php'' および ''image.php'' テンプレートループ内の [[Using Image and File Attachments|attachment]]/[[:en:Using Image and File Attachments|en]] の場合、attachment のキャプションを表示します。キャプションには、抜粋の ''[...]'' 記号は含まれません。 |
<div id="note" class="important"> | <div id="note" class="important"> | ||
16行目: | 16行目: | ||
== the_excerpt() と the_content() == | == the_excerpt() と the_content() == | ||
</div> | </div> | ||
− | + | 敢えて<tt>[[テンプレートタグ/the_content|the_content()]]</tt> 関数を使うことに意味がある場合があります。 それはこの関数が、投稿本文内に [[Writing Posts#Visual Versus HTML_Editor|<tt><nowiki><!--more--></nowiki></tt> タグ]]が使用されているかどうかによって表示内容が変わるためです。<tt><nowiki><!--more--></nowiki></tt> タグは、挿入した位置で本文を2つに分けます。メインブログページやアーカイブといったリスト形式のページではこのタグより前の部分のみを表示します。個別投稿/ページでは(もちろん) <tt><nowiki><!--more--></nowiki></tt> タグが無視され、本文全てを表示します。 | |
<div id="Usage"> | <div id="Usage"> | ||
46行目: | 46行目: | ||
</div> | </div> | ||
− | + | アーカイブページ ([[Conditional Tags#Any Archive Page|<code>is_archive()</code>]] で判定) またはカテゴリーページ ([[Conditional Tags#Any Archive Page|<code>is_category()</code>]] で判定) 表示時に、[[テンプレートタグ/the_content|<code>the_content()</code>]] タグの替わりに <code>the_excerpt()</code> を用いて抜粋文を表示する例です。 | |
<!-- is_archive() にカテゴリページも含まれるので is_category() を消去しました。(ぼの) --> | <!-- is_archive() にカテゴリページも含まれるので is_category() を消去しました。(ぼの) --> | ||
次の例は、WordPress バージョン 1.5 以上で動作します。 | 次の例は、WordPress バージョン 1.5 以上で動作します。 | ||
− | + | <?php if ( is_category() || is_archive() ) { | |
the_excerpt(); | the_excerpt(); | ||
} else { | } else { | ||
57行目: | 57行目: | ||
} ?> | } ?> | ||
− | 1.5 より前のバージョンでは以下のコードで動作します。 | + | <!-- 1.5 より前のバージョンでは以下のコードで動作します。 |
<?php if ( $cat || $m ) { | <?php if ( $cat || $m ) { | ||
63行目: | 63行目: | ||
} else { | } else { | ||
the_content(); | the_content(); | ||
− | } ?> | + | } ?> --> |
<div id="Control_Excerpt_Length_using_Filters"> | <div id="Control_Excerpt_Length_using_Filters"> | ||
69行目: | 69行目: | ||
</div> | </div> | ||
− | デフォルトでは、抜粋の長さは 55 単語です(日本語の場合は[[#note|冒頭の注]]参照)。[[プラグイン API/フィルターフック一覧/excerpt_length|excerpt_length]]/[[:en:Plugin API/Filter Reference/excerpt_length|en]] | + | デフォルトでは、抜粋の長さは 55 単語です(日本語の場合は[[#note|冒頭の注]]参照)。[[プラグイン API/フィルターフック一覧/excerpt_length|excerpt_length]]/[[:en:Plugin API/Filter Reference/excerpt_length|en]] フィルターを使用して長さを20単語に変更するには、テーマの [[テーマの作成#テーマの構造|<code>functions.php</code> ファイル]]に以下のコードを追加します。 |
function new_excerpt_length($length) { | function new_excerpt_length($length) { | ||
return 20; | return 20; | ||
} | } | ||
− | add_filter('excerpt_length', ' | + | add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); |
<div id="Remove_.5B....5D_string_using_Filters"> | <div id="Remove_.5B....5D_string_using_Filters"> | ||
=== フィルターで [...] 文字列の変更・削除 === | === フィルターで [...] 文字列の変更・削除 === | ||
</div> | </div> | ||
+ | WordPress version 2.9では概要の[...]文字列を変更するための新たなフィルターが追加されました。下記メソッドは適切なWordPress versionに対して使用してください。両方を同時に使用しないでください。 | ||
<div id="Only_in_version_2.9_and_higher_of_WordPress"> | <div id="Only_in_version_2.9_and_higher_of_WordPress"> | ||
107行目: | 108行目: | ||
投稿で "read more" リンクを作成するには、テーマの functions.php に以下のコードを挿入してください。 | 投稿で "read more" リンクを作成するには、テーマの functions.php に以下のコードを挿入してください。 | ||
− | function new_excerpt_more($ | + | function new_excerpt_more( $more ) { |
− | return '<a href="'. get_permalink( | + | return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More</a>'; |
− | } | + | } |
− | add_filter('excerpt_more', 'new_excerpt_more'); | + | add_filter( 'excerpt_more', 'new_excerpt_more' ); |
+ | |||
+ | 注: このリンクは新しい行には現れません。CSSをの<tt>read-more</tt>クラスを用いる必要があります。 | ||
<div id="Notes"> | <div id="Notes"> | ||
136行目: | 139行目: | ||
{{No Param Tag Footer}} | {{No Param Tag Footer}} | ||
− | {{原文|Template Tags/the excerpt| | + | {{原文|Template Tags/the excerpt|131710}}<!-- 01:32, 31 May 2013 Narga 版 --> |
{{DEFAULTSORT:The_excerpt}} | {{DEFAULTSORT:The_excerpt}} |
2013年7月6日 (土) 17:08時点における版
目次
説明
このテンプレートタグ the_excerpt()
は、現在の投稿の抜粋を、文末に [...](角括弧+三点リーダー)をつけて表示します。この三点リーダーは「続きを読む」リンクではありません。抜粋を直接表示するために使われますが、get_the_excerpt() から返された文字列に開始と終了の <p> タグを追加します。投稿作成/編集画面 の抜粋文入力欄に記入してあればそれを、抜粋を入力していない場合は投稿内容の最初の 55個の単語までを表示します(注)。また、抜粋のコンテンツからは HTML タグと画像は取り除かれます。このタグはループ内でのみ使えます。
現在の投稿が attachment.php および image.php テンプレートループ内の attachment/en の場合、attachment のキャプションを表示します。キャプションには、抜粋の [...] 記号は含まれません。
(注) 日本語のように語句の間を半角スペースで区切らない言語では「55単語」を判定できないため、抜粋できずに本文のほとんどが出力されてしまうことがあります。
WordPress 日本語版では、この問題を含むマルチバイト関連の問題に対処するために WP Multibyte Patch プラグインを同梱しています。このプラグインを使用すると、110文字まで(設定により変更可)を抜粋文として出力できます。
the_excerpt() と the_content()
敢えてthe_content() 関数を使うことに意味がある場合があります。 それはこの関数が、投稿本文内に <!--more--> タグが使用されているかどうかによって表示内容が変わるためです。<!--more--> タグは、挿入した位置で本文を2つに分けます。メインブログページやアーカイブといったリスト形式のページではこのタグより前の部分のみを表示します。個別投稿/ページでは(もちろん) <!--more--> タグが無視され、本文全てを表示します。
使い方
<?php the_excerpt(); ?>
パラメータ
このタグはパラメータをとりません。
用例
初期設定での使用
投稿の抜粋を表示します。個別投稿を表示する時以外に the_content() の代わりにループ内で強制的に抜粋を表示するときに使用します。
<?php the_excerpt(); ?>
条件分岐タグとの使用
アーカイブページ (is_archive()
で判定) またはカテゴリーページ (is_category()
で判定) 表示時に、the_content()
タグの替わりに the_excerpt()
を用いて抜粋文を表示する例です。
次の例は、WordPress バージョン 1.5 以上で動作します。
<?php if ( is_category() || is_archive() ) {
the_excerpt(); } else { the_content(); } ?>
フィルターで抜粋の長さの変更
デフォルトでは、抜粋の長さは 55 単語です(日本語の場合は冒頭の注参照)。excerpt_length/en フィルターを使用して長さを20単語に変更するには、テーマの functions.php
ファイルに以下のコードを追加します。
function new_excerpt_length($length) { return 20; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
フィルターで [...] 文字列の変更・削除
WordPress version 2.9では概要の[...]文字列を変更するための新たなフィルターが追加されました。下記メソッドは適切なWordPress versionに対して使用してください。両方を同時に使用しないでください。
バージョン 2.9 以降のみ
デフォルトでは、抜粋の最後の文字列は '[...]' になっています。excerpt_more/en フィルターを使用して文字列を変更するには、テーマの functions.php
ファイルに以下のコードを追加します。
function new_excerpt_more($more) { return '[.....]'; } add_filter('excerpt_more', 'new_excerpt_more');
バージョン 2.8.x 以前の場合
function new_excerpt_more($excerpt) { return str_replace('[...]', '...', $excerpt); } add_filter('wp_trim_excerpt', 'new_excerpt_more');
投稿で "read more" リンクを作成する
投稿で "read more" リンクを作成するには、テーマの functions.php に以下のコードを挿入してください。
function new_excerpt_more( $more ) { return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More</a>'; } add_filter( 'excerpt_more', 'new_excerpt_more' );
注: このリンクは新しい行には現れません。CSSをのread-moreクラスを用いる必要があります。
Notes
- Uses: get_the_excerpt/en.
- Uses: apply_filters()/en for 'the_excerpt'.
変更履歴
Since: 0.71
ソースファイル
the_excerpt() は、wp-includes/post-template.php
にあります。
関連
投稿タグ:
body_class(),
next_image_link(),
next_post_link(),
next_posts_link(),
post_class(),
post_password_required(),
posts_nav_link(),
previous_image_link(),
previous_post_link(),
previous_posts_link(),
single_post_title(),
sticky_class(),
the_category(),
the_category_rss(),
the_content(),
the_content_rss(),
the_excerpt(),
the_excerpt_rss(),
the_ID(),
the_meta(),
the_shortlink(),
the_tags(),
the_title(),
the_title_attribute(),
the_title_rss(),
wp_link_pages(),
最新英語版: WordPress Codex » Template Tags/the excerpt (最新版との差分)