Created
September 22, 2015 17:19
-
-
Save dmitry-korolev/7cd1e6aeae994e353769 to your computer and use it in GitHub Desktop.
Get excerpt by id in Wordpress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', ' ' . '[…]' )); | |
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