我已根据您的需要修改了早期的解决方案。
您需要更改以下行
$wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, \'</p>\' ) + 4 );
到这个
$wpse0001_excerpt = explode(\'</p>\', $wpse0001_excerpt, 3);
// unset the rest of the contents
if(isset($wpse0001_excerpt[3])) unset($wpse0001_excerpt[3]);
// append </p> to the end of last paragraph
if(isset($wpse0001_excerpt[2])) $wpse0001_excerpt[1] .= \'</p>\';
// combine all the paragraphs with appending the ending tag.
$wpse0001_excerpt = implode(\'</p>\', $wpse0001_excerpt);
在这里复制整个解决方案。
<?php
if ( ! function_exists( \'wpse0001_custom_wp_trim_excerpt\' ) ) :
function wpse0001_custom_wp_trim_excerpt($wpse0001_excerpt) {
global $post;
$raw_excerpt = $wpse0001_excerpt;
if ( \'\' == $wpse0001_excerpt ) {
$wpse0001_excerpt = get_the_content(\'\');
$wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
$wpse0001_excerpt = apply_filters(\'the_content\', $wpse0001_excerpt);
$wpse0001_excerpt = explode(\'</p>\', $wpse0001_excerpt, 3);
// unset the rest of the contents
if(isset($wpse0001_excerpt[3])) unset($wpse0001_excerpt[3]);
// append </p> to the end of last paragraph
if(isset($wpse0001_excerpt[2])) $wpse0001_excerpt[1] .= \'</p>\';
// combine all the paragraphs with appending the ending tag.
$wpse0001_excerpt = implode(\'</p>\', $wpse0001_excerpt);
// $wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, \'</p>\', strpos( $wpse0001_excerpt, \'</p>\' ) + 4 ) + 4);
$wpse0001_excerpt = str_replace(\']]>\', \']]>\', $wpse0001_excerpt);
$excerpt_end = \' <a href="\'. esc_url( get_permalink() ) . \'">\' . \' » \' . sprintf(__( \'Read more about: %s »\', \'pietergoosen\' ), get_the_title()) . \'</a>\';
$excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end);
//$pos = strrpos($wpse0001_excerpt, \'</\');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$wpse0001_excerpt .= $excerpt_end;
return $wpse0001_excerpt;
}
return apply_filters(\'wpse0001_custom_wp_trim_excerpt\', $wpse0001_excerpt, $raw_excerpt);
}
endif;
remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter(\'get_the_excerpt\', \'wpse0001_custom_wp_trim_excerpt\');
注:未经测试。