get_the_excerpt( int|WP_Post $post = null )
Retrieves the post excerpt.
Description Description
Parameters Parameters
Return Return
(string) Post excerpt.
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 ); }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
4.5.0 | Introduced the $post parameter. |
0.71 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
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.
Use excerpt for HTML meta description
(Setting the second parameter of wp_strip_all_tags to true removes left over line breaks and whitespace chars.)
Use
to prevent a Notice: Undefined offset: -1 in post-template.php
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
Limit Manual Excerpt displayed on Homepage to 260 characters but truncate after the last word
This code snippet must be placed in the theme where
the_excerpt()
is located. This is for modifing manually entered excerpts NOT automatic ones WordPress will grab from the content.It will display the first 260 characters of a manually entered excerpt but instead of ending on the 260th character it will truncate after the closest word. To change the number of characters simply change the value ‘260’ to another number. Remember to remove
the_excerpt()
in your theme and replace it with this:Example
get_the_excerpt()
can be used to retrieve and store the value in a variable, without outputting it to the page.