How to display a brief excerpt of a WordPress post with the_excerpt

The WordPress the_excerpt function is used to display a brief summary or excerpt of a post’s content. It can be useful for providing a teaser of the post’s content on archive pages or in widgets, allowing users to quickly scan through multiple posts and decide which ones to read in full.

By using the_excerpt, website owners can control the length and formatting of the excerpt to ensure that it fits well within the design of their site. This can help improve the overall user experience and encourage visitors to explore more content.

WordPress the_excerpt Function Parameters and Return Value

The the_excerpt function does not accept any parameters. It is designed to automatically generate an excerpt from the content of a post.

Additionally, the the_excerpt function does not return a value. Instead, it directly outputs the generated excerpt to the page where it is called.

Examples

How to display the excerpt of a post

<?php the_excerpt();
?>

This code snippet simply outputs the excerpt of the current post. It can be used within the WordPress loop to display a brief summary of the post content.

How to customize the length of the excerpt

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

This code snippet demonstrates how to customize the length of the excerpt. By using the excerpt_length filter, we can specify the number of words to be displayed in the excerpt.

How to add a “Read more” link to the excerpt

<?php 
function custom_excerpt_more($more) {
 return ' <a href="' . get_permalink() . '">Read more</a>';
}
add_filter('excerpt_more', 'custom_excerpt_more');
the_excerpt();
?>

This code snippet shows how to add a “Read more” link to the end of the excerpt. By using the excerpt_more filter, we can append a link to the full post content after the excerpt.

Conclusion

The the_excerpt function is an effective feature for controlling the display of content on WordPress websites. By using this function, developers can easily customize the length and formatting of excerpts, providing a better user experience for readers. Additionally, the the_excerpt function offers flexibility and control, allowing developers to tailor the display of content to fit the specific needs of their website. With its simple implementation and powerful capabilities, the the_excerpt function is a valuable asset for any WordPress developer.

Related WordPress Functions