The get_the_excerpt() WordPress function

the_excerpt() is a WordPress function used to display a short summary of a post’s content. If you want to retrieve the excerpt as a string, rather than display it directly, you can use the get_the_excerpt() function. Here’s how to use it:

  1. Retrieve the excerpt for a specific post:
bash
$excerpt = get_the_excerpt( $post_id );

Replace $post_id with the ID of the post you want to retrieve the excerpt for. If you don’t specify a post ID, the function will use the current post in the loop.

  1. Display the excerpt:
bash
echo $excerpt;

This code displays the excerpt retrieved in step 1.

You can also use get_the_excerpt() within a loop to retrieve the excerpt for each post:

scss
while ( have_posts() ) {
the_post();
$excerpt = get_the_excerpt();
echo $excerpt;
}

This code displays the excerpt for each post in the loop.

By default, WordPress automatically generates an excerpt for each post based on the first 55 words of the content, but you can customize the excerpt for each post by adding a custom excerpt in the post editor.