Using WordPress ‘get_the_excerpt’ PHP function

The get_the_excerpt() WordPress PHP function retrieves the post excerpt.


$excerpt = get_the_excerpt();
// your custom code here
return $excerpt;


  • $post (int|WP_Post) (Optional) – Post ID or WP_Post object. Default is global $post.
    • Default: null

More information

See WordPress Developer Resources: get_the_excerpt()


Displaying the post excerpt

Display the post excerpt in a theme template.

$excerpt = get_the_excerpt();
echo '<p>' . $excerpt . '</p>';

Customizing the post excerpt length

Change the length of the post excerpt.

function custom_excerpt_length() {
    return 30; // Set the length to 30 words
add_filter('excerpt_length', 'custom_excerpt_length');

$excerpt = get_the_excerpt();
echo '<p>' . $excerpt . '</p>';

Adding a “Read More” link to the post excerpt

Add a “Read More” link to the post excerpt.

$excerpt = get_the_excerpt();
$read_more_link = '<a href="' . get_permalink() . '">Read More</a>';
echo '<p>' . $excerpt . ' ' . $read_more_link . '</p>';

Displaying a custom excerpt text if none is set

Display a custom text if there is no post excerpt.

$excerpt = get_the_excerpt();
if (empty($excerpt)) {
    $excerpt = 'No excerpt available for this post.';
echo '<p>' . $excerpt . '</p>';

Using the post excerpt in a conditional statement

Use the post excerpt in a conditional statement to perform a specific action.

$excerpt = get_the_excerpt();
if (strlen($excerpt) > 100) {
    echo '<p class="long-excerpt">' . $excerpt . '</p>';
} else {
    echo '<p class="short-excerpt">' . $excerpt . '</p>';