X

Get the_excerpt outside loop

Adding this PHP code to your wordpress theme will let you display the post excerpt outside of the loop.


<?php 
   global $post;
   if (is_single()) {
      echo get_the_excerpt($post->ID);
   }
?>

Comments   Leave a Reply

  1. Laurize Albarracin August 17, 2011 at 10:15 am

    I’m not sure, how do I use this and can it be used on wordpress pages?

    1. Hi Laurize, Lets say you wanted to display the excerpt of a post but within the footer of the page. Normally you would need to have the_excerpt within the loop this would let you place it in any location including the side bar footer etc.

      1. Laurize Albarracin August 18, 2011 at 12:32 am

        That’s what I need. but how do I use it? do i just replace the ‘ID’?

        1. Nope it will get the ID of the post it is displayed on so just place it within the sidebar or the header footer etc.

  2. Gwyneth Llewelyn July 5, 2011 at 9:00 pm

    I wish it were that simple — it would be wonderful! Sadly, the usage of get_the_excerpt() with a parameter has been deprecated: look at http://core.trac.wordpress.org/browser/tags/3.2/wp-includes/post-template.php line 261 and following.

    The Codex also flags this usage as deprecated.

  3. This doesn’t work.

    1. Just tested the snippet to make sure I had no errors and it works fine. Can you give me some details on the what you are doing with the snippet ?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!