Languages: English • 関数リファレンス/get the excerpt 日本語 (Add your language)
Returns the excerpt of the post. This is either a user-supplied excerpt, that is returned unchanged, or an automatically generated word-counted trimmed-down version of the full post content.
The auto-generated excerpts have all shortcodes and tags removed which means it is just an unformatted string that would not see any line-breaks in any form of output, since the actual line-breaks in the raw text are also removed.
When called through the_excerpt() this implies that the auto-gen excerpts are just raw text with <p></p> tags wrapped around it, basically.
Auto-gen excerpts have a "more" tag appended to them. This tag is '[…]' by default. It can be changed through the excerpt_more filter.
The default word length is 55 words. It can be changed through the excerpt_length filter.
<?php $excerpt = get_the_excerpt( int|WP_Post $post ) ?>
This function must be used within The Loop when no $post or ID parameter is passed.
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.
$post.
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
?>
Use get_the_excerpt() to print an excerpt by specifying a maximium number of characters.
<?php
the_excerpt_max_charlength(140);
function the_excerpt_max_charlength($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;
}
}
?>
Since: 0.71
get_the_excerpt() is located in wp-includes/post-template.php.