wp_trim_excerpt()
Generates an excerpt from the content, if needed.
Returns a maximum of 55 words with an ellipsis appended if necessary.
The 55 word limit can be modified by plugins/themes using the excerpt_length filter The ' […]' string can be modified by plugins/themes using the excerpt_more filter
Uses: wp_trim_words()
Hooks from the function
Return
String
. The excerpt.
Usage
wp_trim_excerpt( $text, $post );
- $text(string)
- The excerpt. If set to empty, an excerpt is generated.
Default: '' - $post(WP_Post|object|int)
- WP_Post instance or Post ID/object.
Default: null
Examples
#1 Demo
Suppose we have separate quotes for posts and we need to check if the post has such a quote, then we need to output it, and if it does not, then we need to output the trimmed text of the current post:
$excerpt = get_post_meta( $post->ID, 'special_excerpt', 1 ); $text = wp_trim_excerpt( $excerpt ); echo $text;
#2 Output a short text (excerpt) for the post
If we already have an excerpt and we just want to get short of it:
echo "Here is a short info about post: \n" . wp_trim_excerpt( $excerpt );
If there is no ready-made excerpt, you can pass the post ID, then the function will create it from the content:
echo "Here is a short info about post: \n" . wp_trim_excerpt( '', $post_id );
Changelog
Since 1.5.0 | Introduced. |
Since 5.2.0 | Added the $post parameter. |