Get an excerpt for a post outside of the loop - Cobbled Code

The WordPress get_the_excerpt() function does not behave well outside of a loop.

This will do so nicely:

/**
 * Get the excerpt for a post for use outside of the loop.
 *
 * @param int|WP_Post $post ID or WP_Post object.
 * @param int         Optional excerpt length.
 * @return string Excerpt.
 */
function prefix_get_the_excerpt( $post, $excerpt_length = null ) {

	$excerpt = get_post_field( 'post_excerpt', $post );

	if ( '' === $excerpt ) {

		$content = get_post_field( 'post_content', $post );

		// An empty string will make wp_trim_excerpt do stuff we do not want.
		if ( '' !== $content ) {

			$excerpt = strip_shortcodes( $content );

			/** This filter is documented in wp-includes/post-template.php */
			$excerpt = apply_filters( 'the_content', $excerpt );
			$excerpt = str_replace( ']]>', ']]>', $excerpt );

			if ( ! $excerpt_length ) {
				/** This filter is documented in wp-includes/formatting.php */
				$excerpt_length = apply_filters( 'excerpt_length', 55 );
			}

			/** This filter is documented in wp-includes/formatting.php */
			$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );

			$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
		}
	}

	return apply_filters( 'the_excerpt', $excerpt );
}

/**
 * The excerpt for a post for use outside of the loop.
 *
 * @param int|WP_Post $post ID or WP_Post object.
 * @param int         Optional excerpt length.
 */
function prefix_the_excerpt( $post, $excerpt_length = null ) {

	echo wp_kses_post( prefix_get_the_excerpt( $post, $excerpt_length ) );
}

3 thoughts on “Get an excerpt for a post outside of the loop

  1. Just want to say your article is as astounding.
    The clearness for your publish is just cool and i could suppose you are a professional in this subject.
    Well together with your permission let me to grasp your feed to stay
    up to date with drawing close post. Thanks 1,000,000 and please carry on the enjoyable
    work.

Leave a Reply to wentylacja mechaniczna łomianki Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.