get_the_excerpt()
Retrieves the excerpt of current or defined post. By default, should be used in WordPress Loop.
To display post excerpt to the screen, you can use the_excerpt(), it also adds an HTML paragraph (<p></p>) to the post snippet (excerpt).
Used By: the_excerpt_rss(), the_excerpt()
Hooks from the function
Return
String
.
-
If post_excerpt is not specified for the post, then the starting fragment of the current post content will be retrieved. For more information, see wp_trim_excerpt() code. Therefore, to check if the post has an excerpt, use has_excerpt().
-
If "quote" (excerpt) is specified, the value of the "quote" field will be returned.
- For posts protected by a password, always returns a line with the text: "There is no excerpt because this is a protected post.".
Usage
get_the_excerpt( $post );
- $post(int|WP_Post)
- Post ID or WP_Post object.
Default: global $post
Examples
#1 Let's check if there is an excerpt.
If the quote is not specified, then display custom text:
$my_excerpt = get_the_excerpt(); if( $my_excerpt ){ echo wpautop( $my_excerpt ); } else { echo wpautop('Citation not established.'); }
#2 Excerpt output with character limit
Use the get_the_excerpt() function to display a quote with a limited maximum of characters:
function the_excerpt_limited( $charlength ){ $excerpt = get_the_excerpt(); $charlength++; if ( mb_strlen( $excerpt ) > $charlength ) { $subex = mb_substr( $excerpt, 0, $charlength - 5 ); $exwords = explode( ' ', $subex ); $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) ); if ( $excut < 0 ) { echo mb_substr( $subex, 0, $excut ); } else { echo $subex; } echo '[...]'; } else { echo $excerpt; } }
Usage, somewhere in the loop:
<?php the_excerpt_limited( 140 ); ?>
Changelog
Since 0.71 | Introduced. |
Since 4.5.0 | Introduced the $post parameter. |