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:
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
Is this still true by the end of 2023? It’s just that has_excerpt() relates to the user’s manually inserted excerpt (the so-called teaser), while get_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.
Fixing when get_the_excerpt() and the_excerpt() are showing Editor content, when the Editor has been turned off on the post_type
I ran into an issue with some legacy content in a custom post type, that originally had the content 'editor' in the register_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 where get_the_excerpt() or the_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 the post_type, and then try to find a suitable alternative field in post_meta. It borrows code from wp_trim_excerpt() to both trim length and then add the [...] as would be expected for the get_the_excerpt() and the_excerpt() results.
/**
* WordPress get_the_excerpt() and the_excerpt() do not check if the 'editor' is turned off on the post_type or not,
* so vestigial content can be displayed. This checks if the 'editor' is turned on for the $post->post_type, and tries
* to find a suitable alternative field if applicable.
*/
function wpdocs_custom_excerpt( $post_excerpt, $post ) {
$supports_editor_content = post_type_supports( $post->post_type, 'editor' );
if ( ! $supports_editor_content ) {
$post_meta = get_post_meta( $post->ID );
// Try to find alternative field to pull an excerpt replacement from...
$post_excerpt = $post_meta['overview'][0] ?? $post_meta['description'][0] ?? ''; // <-- Example: Customize this code here for your theme
if ( '' !== $post_excerpt ) {
// Trimming to excerpt_length via code lifted from wp_trim_excerpt...
// Ref: https://developer.wordpress.org/reference/functions/wp_trim_excerpt/
$excerpt_length = (int) _x( '55', 'excerpt_length' );
$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
$excerpt_more = apply_filters( 'excerpt_more', ' […]' );
$post_excerpt = wp_trim_words( $post_excerpt, $excerpt_length, $excerpt_more );
}
}
return is_string( $post_excerpt ) ? $post_excerpt : '';
}
add_filter( 'get_the_excerpt', 'wpdocs_custom_excerpt', 10, 2 );
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.
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.