the_excerpt

Sponsored Link

最後に[…]がある現在の投稿の抜粋を表示します。”read more”リンクではありません。投稿(投稿エディタのオプションの抜粋フィールド)に明確な抜粋を提供していなかった場合、投稿のコンテンツの最初の55の単語を参照する自動抜粋が表示されます。
また後者の場合、HTMLタグとグラフィックが抜粋コンテンツから削除されます。このタグはループ内にある必要があります。

注:現在の投稿が添付だった場合、例えばattachment.php と image.phpのテンプレートでループしているなどの場合は、添付ファイルのキャプションが表示されます。キャプションは、[…]マークの抜粋を含みません。

Sponsored Link

Displays the excerpt of the current post with […] at the end, which is not a “read more” link. If you do not provide an explicit excerpt to a post (in the post editor’s optional excerpt field), it will display an automatic excerpt which refers to the first 55 words of the post’s content. Also in the latter case, HTML tags and graphics are stripped from the excerpt’s content. This tag must be within The Loop.

Note: If the current post is an attachment, such as in the attachment.php and image.php template loops, then the attachment caption is displayed. Captions do not include the excerpt […] marks.

the_excerpt() vs. the_content()

場合によっては、the_content()関数のみ使用するためにより意味があります。 the_content()はタグが使用されたかどうかに応じて表示するために決定されます。 タグは、投稿/ページの2部に分割します:コンテンツの前の場合のみタグが一覧に表示される必要があります。が(もちろん)はシングル投稿/ページを表示している時は無視されるのを忘れないでください。

Sometimes it is more meaningful to use only the_content() function. the_content() will decide what to display according to whether tag was used. The tag splits post/page into two parts: only content before the tag should be displayed in listing. Remember that is (of course) ignored when showing single post/page.

the_excerptのテンプレートタグ使用方法

<?php the_excerpt(); ?> 

the_excerptのパラメーター

このタグにはパラーメータはありません。

This tag has no parameters.

デフォルトの使い方

記事の抜粋を表示します。 non-single/non-permalinkの投稿上では、ループ内で表示する抜粋を強制するthe_content()の代わりとして使用されます。

Displays the post excerpt. Used on non-single/non-permalink posts as a replacement for the_content() to force excerpts to show within the Loop.

<?php the_excerpt(); ?>

条件タグと使用する場合

アーカイブ(tested by is_archive() )やカテゴリー(tested by is_category() )ページ上ではthe_content()タグがthe_excerpt()に置き換えます。

Replaces the_content() tag with the_excerpt() when on archive (tested by is_archive() ) or category (tested by is_category() ) pages.

両方の例は、バージョン1.5以上の動作を表示します。

Both the examples below work for versions 1.5 and above.

<?php if ( is_category() || is_archive() ) {
	the_excerpt();
} else {
	the_content();
} ?>

WordPressバージョンが1.5より前では、次のみ動作するでしょう:

For versions of WordPress prior to 1.5, only the following will work :

<?php if ( $cat || $m ) {
	the_excerpt();
} else {
	the_content();
} ?>

フィルターを使用して抜粋の長さをコントロールする方法

デフォルトでは、抜粋の長さ55語に設定されています。 excerpt_lengthフィルターを使用して抜粋の長さを変更するには、次のコードをテーマファイルのfunctions.phpに追加してください:

By default, excerpt length is set to 55 words. To change excerpt length using excerpt_length filter, add the following code to functions.php file in your theme:

function new_excerpt_length($length) {
	return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');

フィルターを使用して[…]文字列を削除する方法

WordPressの2.9以上のバージョンのみ

デフォルトでは、最後に抜粋のmoreの文字列は、'[…]’.に設定されます。excerpt_moreフィルタを使用して抜粋のmore文字列を変更するには、次のコードをテーマファイルのfunctions.phpに追加します:

By default, excerpt more string at the end is set to ‘[…]’. To change excerpt more string using excerpt_more filter, add the following code to functions.php file in your theme:

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に配置します。

Place this in a theme’s functions.php to make the “read more” link to the post

function new_excerpt_more($post) {
	return '<a href="'. get_permalink($post->ID) . '">' . 'Read the Rest...' . '</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

注釈

  • Uses: get_the_excerpt.
  • Uses: apply_filters() for ‘the_excerpt’.

変更ログ

Since: 0.71

ソースファイル

the_excerpt()は、wp-includes/post-template.php内に位置しています。

the_excerpt() is located in wp-includes/post-template.php.

関連テンプレートタグ

the_ID, the_title, the_title_attribute, single_post_title, the_title_rss, the_content, the_content_rss, the_excerpt, the_excerpt_rss, wp_link_pages, next_post_link, next_posts_link, previous_post_link, previous_posts_link, posts_nav_link, sticky_class, the_meta

Sponsored Link