您可以提供自己的实现wp_trim_excerpt
它负责修剪和剥离HTML标记(它使用wp_strip_all_tags
),
通过复制函数源代码并应用所需的更改(即保留<p>
和<strong>
标签(或您希望的任何其他标签),它将很好地工作。
我为您复制了函数并应用了允许的更改<p>
和<strong>
在你最后的摘录中。(You should put this code in your functions.php
file)
function wp_trim_excerpt_modified($text, $content_length = 55, $remove_breaks = false) {
if ( \'\' != $text ) {
$text = strip_shortcodes( $text );
$text = excerpt_remove_blocks( $text );
$text = apply_filters( \'the_content\', $text );
$text = str_replace(\']]>\', \']]>\', $text);
$num_words = $content_length;
$more = $excerpt_more ? $excerpt_more : null;
if ( null === $more ) {
$more = __( \'…\' );
}
$original_text = $text;
$text = preg_replace( \'@<(script|style)[^>]*?>.*?</\\\\1>@si\', \'\', $text );
// Here is our modification
// Allow <p> and <strong>
$text = strip_tags($text, \'<p>,<strong>\');
if ( $remove_breaks )
$text = preg_replace(\'/[\\r\\n\\t ]+/\', \' \', $text);
$text = trim( $text );
if ( strpos( _x( \'words\', \'Word count type. Do not translate!\' ), \'characters\' ) === 0 && preg_match( \'/^utf\\-?8$/i\', get_option( \'blog_charset\' ) ) ) {
$text = trim( preg_replace( "/[\\n\\r\\t ]+/", \' \', $text ), \' \' );
preg_match_all( \'/./u\', $text, $words_array );
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
$sep = \'\';
} else {
$words_array = preg_split( "/[\\n\\r\\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
$sep = \' \';
}
if ( count( $words_array ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
} else {
$text = implode( $sep, $words_array );
}
}
return $text;
}
请注意正在执行剥离的行:
Line:22 $text = strip_tags($text, \'<p>,<strong>\');
您的代码应该如下所示:
$project_desc = get_field( \'project_description\' );
if( !empty( $project_desc ) ):
$trimmed_text = wp_trim_excerpt_modified( $project_desc, 15 );
$last_space = strrpos( $trimmed_text, \' \' );
$modified_trimmed_text = substr( $trimmed_text, 0, $last_space );
echo $modified_trimmed_text . \'...\';
endif;
有关更多信息,您可以签出
this answer 关于stackoverflow,它更为详细。