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'];
}
it does not work in 3.8
any help plz....
can u plz explain where do i have to put this code?
This code would be used in a template anywhere outside of "the loop", for example the header, footer or sidebars.
Thanks for posting this. Seems obvious really, one would assume that for something so obviously useful, WP would provide a function for it!
Brilliant, thank you!
How do I pass the post ID? thanks
Perfect! Just what I was looking for
Cheers
D
Hmm. What if I need an automatically generated excerpt? It won't be stored on the database.
Is there any way to have this pull an excerpt of the most recent post instead of specifying an ID?
@estevan, Hello dear, you can easily get the latest post excerpt using the following query
SELECT post_excerpt FROM $wpdb->posts where `post_status`='publish' && post_excerpt!='' && post_password='' && (post_type!='post' OR post_type!='page') ORDER BY `ID` DESC LIMIT 1
Have fun and Bye
Thanks so much for this, I've been searching for a solution to this for some time.