Wordpress Get the Excerpt Outside the Loop

How 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.

By Tim TrottWordPress • January 24, 2009

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.

php
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'];
}

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 11 comments. Why not join the discussion!

New comments for this post are currently closed.