has_excerpt()
Whether the post has a custom excerpt.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
No Hooks.
Return
true|false
. True if the post has a custom excerpt, false otherwise.
Usage
has_excerpt( $post );
- $post(int|WP_Post)
- Post ID or WP_Post object.
Default: global $post
Examples
#1 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(); }
#2 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. }
Changelog
Since 2.3.0 | Introduced. |
has_excerpt() has excerpt code WP 6.7.1
function has_excerpt( $post = 0 ) { $post = get_post( $post ); return ( ! empty( $post->post_excerpt ) ); }