get_the_excerpt( int|WP_Post $post = null )

Retrieves the post excerpt.


Description Description


Parameters Parameters

$post

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

Default value: null


Top ↑

Return Return

(string) Post excerpt.


Top ↑

Source Source

File: wp-includes/post-template.php

function get_the_excerpt( $post = null ) {
	if ( is_bool( $post ) ) {
		_deprecated_argument( __FUNCTION__, '2.3.0' );
	}

	$post = get_post( $post );
	if ( empty( $post ) ) {
		return '';
	}

	if ( post_password_required( $post ) ) {
		return __( 'There is no excerpt because this is a protected post.' );
	}

	/**
	 * Filters the retrieved post excerpt.
	 *
	 * @since 1.2.0
	 * @since 4.5.0 Introduced the `$post` parameter.
	 *
	 * @param string $post_excerpt The post excerpt.
	 * @param WP_Post $post Post object.
	 */
	return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.5.0 Introduced the $post parameter.
0.71 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Mikko Saari

    If this function is used outside The Loop and the post doesn’t have a custom excerpt, this function will use wp_trim_excerpt() to generate an excerpt. That function uses get_the_content(), which must be used with The Loop and will cause problems if get_the_excerpt() is being used outside The Loop. In order to avoid the issues, use setup_postdata() prior to calling get_the_excerpt() to set up the global $post object.

  2. Skip to note 2 content
    Contributed by Barrett Golding

    Use excerpt for HTML meta description

    <!-- Use Post excerpt for meta description.  -->
    <?php if ( is_single() ) { ?>
    <meta name="description" content="<?php echo wp_strip_all_tags( get_the_excerpt(), true ); ?>" />
    <?php } ?>
    

    (Setting the second parameter of wp_strip_all_tags to true removes left over line breaks and whitespace chars.)

  3. Skip to note 3 content
    Contributed by Samet Tarim

    Use

    has_excerpt()

    to prevent a Notice: Undefined offset: -1 in post-template.php

    $excerpt = '';
    if (has_excerpt()) {
        $excerpt = wp_strip_all_tags(get_the_excerpt());
    }

    Otherwise, you could get an Undefined offset -1 warning, do not try to get_excerpt directly to check whether with isset or NULL, you’ll get an undefined offset -1 back

  4. Skip to note 4 content
    Contributed by Codex

    Example
    get_the_excerpt() can be used to retrieve and store the value in a variable, without outputting it to the page.

    <?php
    $my_excerpt = get_the_excerpt();
    if ( '' != $my_excerpt ) {
    	// Some string manipulation performed
    }
    echo $my_excerpt; // Outputs the processed value to the page
    ?>
    

You must log in before being able to contribute a note or feedback.