How to Display the Excerpt in WordPress

What is the excerpt?

The excerpt is a description or small portion of a blog post. Most of the time it is used on the main blog page to show small teasers for your most recent posts.

The excerpt needs to be intriguing and exciting to persuade users to click through to the full post. WordPress gives you many different ways to display the excerpt for a post.

Here are some different ways you can display and manipulate the excerpt on your WordPress blog.

1. Use the_excerpt() function

The first way to display the excerpt is to place the function the_excerpt() within The Loop. The function will automatically display the first 55 words of the post’s content. It will also remove HTML tags and graphics.

This function can also be used to display a custom excerpt provided in the post editor’s excerpt field. If a custom excerpt is provided, it will override the automatic excerpt. If you can’t see this field in your post editor, click on Screen Options in the top right corner of your screen and make sure Excerpt is checked.

WordPress Excerpt Field

2. Changing the default excerpt length

The default excerpt length when using the_excerpt() function is 55 words. If you want to change this length to something shorter or longer, just use the excerpt_length filter.

Place the following function in your functions.php file, changing out ’20’ for however many words you would like to display.

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

3. Add a ‘read more’ link to the end of the excerpt

By default the excerpt displays ‘[…]’ at the end of the excerpt, with no link to the full post. To add a link, use the excerpt_more filter.

Place the following function in your functions.php file.

function read_more_excerpt( $more ) {
     return ' <a class"read-more" href"' . get_permalink( get_the_ID() ) . '">Read More</a>';
}
add_filter( 'excerpt_more', 'read_more_excerpt' );

4. Use the ‘More’ tag

Using this tag in the post editor to split the post into two parts. All content before the tag will be displayed as the excerpt, including HTML tags and graphics. In the content editor you can click on the ‘more’ button in the toolbar to place this tag within the content.

For this tag to work, use the_content() instead of the_excerpt() in The Loop.

WordPress More Tag

Learn more about the WordPress excerpt on the Codex.

Leave a Reply

Your email address will not be published. Required fields are marked *