这是我对以帖子对象或帖子ID为参数的“ trim_excerpt”的看法。
显然基于核心内容。不知道为什么这个(和get_the_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);
}
$myvar = apply_filters( 'the_excerpt', $myvar );