has_excerpt()
Checks if the current post has an excerpt (quote, short description). Conditional tag.
No Hooks.
Returns
true|false. true or false, depending on whether there is a quote or not.
Usage
if( has_excerpt( $id ) ){
// ...
}
- $id(number)
- ID of the post to check for the presence of a quote.
Default: current post
Examples
#1 Test for presence of excerpt in a post.
// Get $post if you're inside a php function.
global $post;
if ( has_excerpt( $post->ID ) ) {
// This post has excerpt.
} else {
// This post has no excerpt.
} #2 Check if the post has a short description
When displaying posts in the loop, we check if the post has a short description, if it does, we display it, if not, we display the content:
if( has_excerpt() ){
the_excerpt();
}
else {
the_content();
}
Changelog
| Since 2.3.0 | Introduced. |
has_excerpt() has excerpt code WP 6.9
function has_excerpt( $post = 0 ) {
$post = get_post( $post );
return ( ! empty( $post->post_excerpt ) );
}