WordPress在中删除标记wp_trim_words()
, 调用的get_the_excerpt()
; 因此,我们必须过滤“wp\\u trim\\u words”,基本上只需一个更改即可复制该函数:replacewp_strip_all_tags()
具有strip_tags()
.
我们不希望其他函数运行wp_trim_words
要修改,所以我们在get_the_excerpt()
正在运行,完成后将其删除。
// Allow links in excerpts
function sg_trim_words( $text, $num_words, $more, $original_text ) {
$text = strip_tags( $original_text, \'\' );
// @See wp_trim_words in wp-includes/formatting.php
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 );
}
// Remove self so we don\'t affect other functions that use wp_trim_words
remove_filter( \'wp_trim_words\', \'sg_trim_words\' );
return $text;
}
// Be sneaky: add our wp_trim_words filter during excerpt_more filter, which is called immediately prior
function sg_add_trim_words_filter( $excerpt_length ) {
add_filter( \'wp_trim_words\', \'sg_trim_words\', 10, 4 );
return $excerpt_length;
}
add_filter( \'excerpt_more\', \'sg_add_trim_words_filter\', 1 );
我写道
this gist 在回顾了其他建议的方法之后,因为我认为这是一个更有针对性的解决方案。今后将更新要点。