Skip to content

Instantly share code, notes, and snippets.

@jimboobrien
Forked from wpscholar/wp-get-excerpt-by-id.php
Created September 20, 2017 23:18
Show Gist options
  • Save jimboobrien/be25c7435c58cac63bb3e4651add3475 to your computer and use it in GitHub Desktop.
Save jimboobrien/be25c7435c58cac63bb3e4651add3475 to your computer and use it in GitHub Desktop.
Get the excerpt for a WP_Post by post ID.
<?php
/**
* Get the excerpt for a WP_Post by post ID.
*
* @param int $post_id
*/
function get_excerpt_by_id( $post_id = 0 ) {
global $post;
$save_post = $post;
$post = get_post( $post_id );
setup_postdata( $post );
$excerpt = get_the_excerpt();
$post = $save_post;
wp_reset_postdata( $post );
return $excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment