Retrieves the post excerpt.
Parameters
Source
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 );
}
Hooks
- apply_filters( ‘get_the_excerpt’,
string $post_excerpt ,WP_Post $post ) Filters the retrieved post excerpt.
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
[html]
<!– 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 } ?>
[/html]
(Setting the second parameter of wp_strip_all_tags to true removes left over line breaks and whitespace chars.)
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: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
has_excerpt()
relates to the user’s manually inserted excerpt (the so-called teaser), whileget_the_excerpt()
allegedly either returns the user’s teaser excerpt if it exists, or automatically generates an automated excerpt (with tags stripped). At least, that’s how it looks like when viewing the source code as of writing this.Expandable excerpt function
first the PHP
Add this function in function.php
Required CSS
CSS to simply hide elements when needed
.excerpt-full.hide {
display: none;
}
.see-more-text.hide {
display: none;
}
required JS
script to add/remove css classes when needed
Fixing when
get_the_excerpt()
andthe_excerpt()
are showing Editor content, when the Editor has been turned off on thepost_type
I ran into an issue with some legacy content in a custom post type, that originally had the content
'editor'
in theregister_post_type()
arguments'supports' => (..., 'editor')
, but had since been removed in favor of using some custom fields to better standardize the content structure in the templates. This had the result of displaying vestigial content from the editor in the templates whereget_the_excerpt()
orthe_excerpt()
were called. The following is a filter to catch the$post_excerpt
string before it gets returned, and to both check if'editor'
is turned off on thepost_type
, and then try to find a suitable alternative field inpost_meta
. It borrows code fromwp_trim_excerpt()
to both trim length and then add the[...]
as would be expected for theget_the_excerpt()
andthe_excerpt()
results.Example
get_the_excerpt()
can be used to retrieve and store the value in a variable, without outputting it to the page.