Get excerpt from $post->post_content

When in the loop, this will produce excerpt from $post->post_content directly:

<?php echo wp_trim_excerpt(); ?>

Read more HERE.

Alternative Solution:

If you are not in the loop, then, you may use similar implementation as done in the wp_trim_excerpt function:

$text = strip_shortcodes( $post->post_content );
$text = apply_filters( 'the_content', $text );
$text = str_replace(']]>', ']]&gt;', $text);
$excerpt_length = apply_filters( 'excerpt_length', 55 );
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
echo $text;

Leave a Comment