下面是我对“trim\\u摘录”的看法,它将post对象或post ID作为参数。
显然是基于核心的。不知道为什么这个(和get\\u the\\u author())没有非循环等价物。
/**
* Generates an excerpt from the content, if needed.
*
* @param int|object $post_or_id can be the post ID, or the actual $post object itself
* @param string $excerpt_more the text that is applied to the end of the excerpt if we algorithically snip it
* @return string the snipped excerpt or the manual excerpt if it exists
*/
function zg_trim_excerpt($post_or_id, $excerpt_more = \' [...]\') {
if ( is_object( $post_or_id ) ) $postObj = $post_or_id;
else $postObj = get_post($post_or_id);
$raw_excerpt = $text = $postObj->post_excerpt;
if ( \'\' == $text ) {
$text = $postObj->post_content;
$text = strip_shortcodes( $text );
$text = apply_filters(\'the_content\', $text);
$text = str_replace(\']]>\', \']]>\', $text);
$text = strip_tags($text);
$excerpt_length = apply_filters(\'excerpt_length\', 55);
// don\'t automatically assume we will be using the global "read more" link provided by the theme
// $excerpt_more = apply_filters(\'excerpt_more\', \' \' . \'[...]\');
$words = preg_split("/[\\n\\r\\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(\' \', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(\' \', $words);
}
}
return apply_filters(\'wp_trim_excerpt\', $text, $raw_excerpt);
}