你可以尝试第二种选择,但我认为这太过分了。相反,只需编写自己的自定义模拟wp_trim_excerpt()
(应用excerpt_length
和excerpt_more
过滤到摘录),如下所示:
function custom_excerpt( $new_length = 20, $new_more = \'...\', $strip = false ) {
// Start with the content
$text = get_the_content(\'\');
// Do stuff to it
$text = strip_shortcodes( $text );
$text = apply_filters(\'the_content\', $text);
$text = str_replace(\']]>\', \']]>\', $text);
// Use custom values
$excerpt_length = $new_length;
$excerpt_more = $new_more;
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
// Strip?
if ( ! $strip ) {
$text = \'<p>\' . $text . \'</p>\';
}
// Output
echo $text;
}
溢流方法:
function custom_excerpt( $new_length = 20, $new_more = \'...\', $strip = false ) {
// Excerpt length
add_filter( \'excerpt_length\', \'custom_excerpt_length\', 999 );
function custom_excerpt_length( $new_length ) {
return $new_length;
}
// Excerpt More
add_filter( \'excerpt_more\', \'custom_excerpt_more\' );
function custom_excerpt_more( $new_more ) {
return $new_more;
}
// Output
$output = get_the_excerpt();
$output = apply_filters(\'wptexturize\', $output);
$output = apply_filters(\'convert_chars\', $output);
if(!$strip)
$output = \'<p>\' . $output . \'</p>\';
echo $output;
}