0
0
WordpressHow-ToBeginner · 4 min read

How to Add Excerpt in WordPress: Simple Guide

To add an excerpt in WordPress, use the Excerpt box in the post editor to write a summary manually or let WordPress generate it automatically. To display excerpts on your site, use the the_excerpt() function in your theme templates.
📐

Syntax

The main syntax to display an excerpt in WordPress theme files is using the the_excerpt() function. This function outputs the post excerpt or an automatic summary if no manual excerpt is set.

Example usage in a theme template:

  • <?php the_excerpt(); ?> - Displays the excerpt for the current post.
php
<?php
the_excerpt();
?>
Output
Displays the post excerpt or an automatic summary of the post content.
💻

Example

This example shows how to add a manual excerpt in the WordPress editor and display it in a theme template.

First, in the WordPress admin post editor, enable the Excerpt box from the Screen Options tab if not visible. Then write your summary in the Excerpt box.

Next, in your theme's single.php or content.php file, add the code below to show the excerpt:

php
<?php
if (has_excerpt()) {
    the_excerpt(); // Show manual excerpt if set
} else {
    echo wp_trim_words(get_the_content(), 40, '...'); // Fallback automatic excerpt
}
?>
Output
Shows the manual excerpt if available; otherwise, shows the first 40 words of the post content followed by ellipsis.
⚠️

Common Pitfalls

Common mistakes when adding excerpts in WordPress include:

  • Not enabling the Excerpt box in the post editor, so you can't add manual excerpts.
  • Using the_content() instead of the_excerpt() when you want a summary.
  • Expecting the_excerpt() to always show a manual excerpt; if none is set, it auto-generates a summary which may cut off mid-sentence.
  • Not styling excerpts in CSS, which can make them look inconsistent.

Example of wrong vs right usage:

php
<?php
// Wrong: shows full content instead of excerpt
// echo the_content();

// Right: shows excerpt summary
if (has_excerpt()) {
    the_excerpt();
} else {
    echo wp_trim_words(get_the_content(), 40, '...');
}
?>
Output
Correctly displays a summary excerpt instead of full post content.
📊

Quick Reference

ActionFunction/MethodDescription
Display excerptthe_excerpt()Outputs manual excerpt or auto summary
Check if manual excerpt existshas_excerpt()Returns true if manual excerpt is set
Get post contentget_the_content()Retrieves full post content
Trim wordswp_trim_words()Limits text to specified word count with optional ending
Enable excerpt boxScreen Options in editorShow excerpt input box in post editor

Key Takeaways

Use the_excerpt() in theme files to display post excerpts.
Add manual excerpts in the WordPress editor by enabling the Excerpt box.
If no manual excerpt exists, WordPress auto-generates a summary.
Use has_excerpt() to check if a manual excerpt is set before displaying.
Avoid using the_content() when you want a short summary.