Answers:
这需要PHP 5.3+(WP需要PHP 5.2.4+)
add_filter('get_the_excerpt', 'end_with_sentence');
function end_with_sentence($excerpt) {
$allowed_end = array('.', '!', '?', '...');
$exc = explode( ' ', $excerpt );
$found = false;
$last = '';
while ( ! $found && ! empty($exc) ) {
$last = array_pop($exc);
$end = strrev( $last );
$found = in_array( $end{0}, $allowed_end );
}
return (! empty($exc)) ? $excerpt : rtrim(implode(' ', $exc) . ' ' .$last);
}
在@kaiser评论之后,我尝试在保存/更新上运行此命令,以防止页面显示很多帖子时变慢。这应该进行测试
add_filter('wp_insert_post_data', 'end_with_sentence_on_save', 20, 2);
function end_with_sentence_on_save($data, $postarr) {
if ( ! empty( $data['post_content'] ) && $data['post_status'] != 'inherit' && $data['post_status'] != 'trash' ) {
$text = strip_shortcodes( $data['post_content'] );
$text = apply_filters('the_content', $text );
$text = str_replace(']]>', ']]>', $text );
$excerpt_length = apply_filters('excerpt_length', 55);
$data['post_excerpt'] = wp_trim_words($text, $excerpt_length, '');
} else {
return $data;
}
$allowed_end = array('.', '!', '?', '...');
$exc = explode(' ', $data['post_excerpt']);
$found = false;
$last = '';
while ( ! $found && ! empty($exc) ) {
$last = array_pop($exc);
$end = strrev( $last );
$found = in_array( $end{0}, $allowed_end );
}
if (! empty($exc)) $data['post_excerpt'] = rtrim(implode(' ', $exc) . ' ' .$last);
return $data;
}
$end{0}
?顺便说一句,您可以返回或break
找到东西。补充说明:最好在保存帖子时这样做,这样它就不会在运行时重新生成,如果显示很多帖子,它会变得很慢。
$found
I中断循环并检查最后一个字符串,因此无需break
。
while
检查false
。+1
这是我的版本,保留所有html标记,并在句子的最后一个单词后修剪内容
if ( ! function_exists( 'pietergoosen_custom_wp_trim_excerpt' ) ) :
function pietergoosen_custom_wp_trim_excerpt($pietergoosen_excerpt) {
global $post;
$raw_excerpt = $pietergoosen_excerpt;
if ( '' == $pietergoosen_excerpt ) {
$pietergoosen_excerpt = get_the_content('');
$pietergoosen_excerpt = strip_shortcodes( $pietergoosen_excerpt );
$pietergoosen_excerpt = apply_filters('the_content', $pietergoosen_excerpt);
$pietergoosen_excerpt = str_replace(']]>', ']]>', $pietergoosen_excerpt);
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $pietergoosen_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_word_count && preg_match('/[\?\.\!]\s*$/uS', $token)) {
// Limit reached, continue until ? . or ! occur at the end
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what's left of the token
$excerptOutput .= $token;
}
$pietergoosen_excerpt = trim(force_balance_tags($excerptOutput));
$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($pietergoosen_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$pietergoosen_excerpt = substr_replace($pietergoosen_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$pietergoosen_excerpt .= $excerpt_end;
return $pietergoosen_excerpt;
}
return apply_filters('pietergoosen_custom_wp_trim_excerpt', $pietergoosen_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt');