Skip to content

Instantly share code, notes, and snippets.

@dmitry-korolev
Created September 22, 2015 17:19
Show Gist options
  • Save dmitry-korolev/7cd1e6aeae994e353769 to your computer and use it in GitHub Desktop.
Save dmitry-korolev/7cd1e6aeae994e353769 to your computer and use it in GitHub Desktop.
Get excerpt by id in Wordpress.
<?php
if (!function_exists(get_excerpt_by_id)) {
/**
* Returns excerpt by ID. WP_Post->post_excerpt if it presents, wp_trim_words on other case.
* @param mixed $post WP_Post object or post ID. Accepts WP_Post object or post ID (integer or string);
* @return string Excerpt
*/
function get_excerpt_by_id($post) {
$return_excerpt = function($post) {
if ($post->post_excerpt == '')
return wp_trim_words($post->post_content, apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ));
return $post->post_excerpt;
};
if (!is_object($post)) {
$post = get_post($post);
}
return apply_filters('the_excerpt', $return_excerpt($post));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment