Wordpress Get the Excerpt Outside the LoopHow to get a WordPress post excerpt when not in the loop or you don't have the post variable in scope. This will get the excerpt anywhere.
The WordPress template tag the_excerpt
and get_the_excerpt
are great for showing a short summary of a post. Unfortunately, you cannot use get_the_excerpt
outside "the loop". This is a similar function that can be used anywhere in your theme or template.
This snippet can even be used to display the excerpt for any post anywhere on any page. Just pass in the post ID of the post to show the excerpt for.
function get_the_excerpt_here($post_id)
{
global $wpdb;
$query = "SELECT post_excerpt FROM $wpdb->posts WHERE ID = $post_id LIMIT 1";
$result = $wpdb->get_results($query, ARRAY_A);
return $result[0]['post_excerpt'];
}