WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/the excerpt

Contents

Description

Displays the excerpt of the current post with the "[...]" text at the end, which is not a "read more" link. Used to directly display the excerpt, but it adds opening and closing <p> tags to the string returned by get_the_excerpt(). 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 template 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 "[...]" text.

the_excerpt() vs. the_content()

Sometimes, it is more meaningful to use only the the_content() function which will decide what to display according to whether the <!--more--> quicktag was used. The <!--more--> quicktag splits a post into two parts; only the content before the tag should be displayed in listing. Remember that <!--more--> is (of course) ignored when showing a single post.

Usage

 <?php the_excerpt(); ?> 

Parameters

This tag has no parameters.

Examples

Default Usage

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(); ?>

Use with Conditional Tags

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

Both the examples below work for versions 1.5 and above.

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

Control Excerpt Length using Filters

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

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

Remove [...] string using Filters

In WordPress version 2.9, a new filter was added to make changing the [...] string in the excerpt. Do not use both of these methods in the same WordPress installation. Use the one appropriate for your WordPress version.

For versions 2.9 and higher of WordPress

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');

For versions 2.8.x and Older

function new_excerpt_more( $excerpt ) {
	return str_replace( '[...]', '...', $excerpt );
}
add_filter( 'wp_trim_excerpt', 'new_excerpt_more' );

Make the "read more" link to the post

Place this in a theme's functions.php to make the "read more" link to the post

function new_excerpt_more( $more ) {
	return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __('Read More', 'your-text-domain') . '</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

Note: This link will not appear in a new line. You'll need to use CSS on the read-more class.

Notes

Change Log

Since: 0.71

Source File

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

Related

get_the_excerpt(),

See also index of Function Reference and index of Template Tags.