If you’re trying to get a posts excerpt from outside the loop, you may run into problems, as unlike most other WordPress content call functions, get_the_excerpt takes only a post object, and will not work with a post ID. If you find yourself in a situation where you only have access to the post ID and not the whole post object, adding this function to functions.php and calling it instead of get_the_excerpt() with the post ID as the argument will give you the excerpt.


All this function is doing is getting the post object using the post ID ($post_id)



This function assigns all of the global variables that are used in regular template tags (like get_the_excerpt).



The rest of the function grabs the excerpt, resets the post variable and returns the excerpt.