WordPress the_excerpt() Function

Definition
In WordPress, the_excerpt() prints a short summary of the current post. It is perfect for blog lists and search results.
If you wrote an excerpt in the editor, that text is used. If not, WordPress makes one from the first 55 words of the post.
Syntax
A few ways to call the_excerpt().
| Call | What it does |
|---|
the_excerpt() | Print the excerpt. |
get_the_excerpt() | Return the excerpt as a string. |
add_filter( 'excerpt_length', ... ) | Change how many words the automatic excerpt has. |
add_filter( 'excerpt_more', ... ) | Change the text at the end of the excerpt. |
It prints the excerpt inside a paragraph. It takes no arguments.
Full declaration
Parameters
| Parameter | Description |
|---|
(none) | This function takes no parameters. |
Return Value
| Return Value | Description |
|---|
| void | Nothing is returned. The excerpt is printed. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Excerpt in a blog list
Show the title and a short summary for each post.
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<h2>Hello World</h2>
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing! […]</p>
How It Works
- The Loop goes through each post.
the_excerpt() prints the short summary in a p tag.- The
[…] is added when the text was cut.
Example 2 — Make the excerpt shorter
Use the excerpt_length filter to change the word count.
<?php
function mytheme_excerpt_length() {
return 20;
}
add_filter( 'excerpt_length', 'mytheme_excerpt_length' );
<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing! […]</p>
How It Works
- The filter returns the number of words.
- Here the automatic excerpt is 20 words long.
- This does not change excerpts you wrote by hand.
Example 3 — Change the ending
Use excerpt_more to change the text at the end.
<?php
function mytheme_excerpt_more() {
return '... <a href="' . esc_url( get_permalink() ) . '">Read more</a>';
}
add_filter( 'excerpt_more', 'mytheme_excerpt_more' );
<p>Welcome to WordPress. This is your first post. ... <a href="https://example.com/hello-world/">Read more</a></p>
How It Works
- The filter returns the text added after a cut excerpt.
get_permalink() gives the link to the post.- The new ending replaces
[…].
Frequently Asked Questions
It prints a short summary of the current post.
From the Excerpt box in the editor, or from the first 55 words of the post if the box is empty.
the_excerpt() prints a summary. the_content() prints the full post.
Use the excerpt_length filter.
Use the excerpt_more filter.
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful