explode, array_filter, array_slice, 和implode 可以通过查看空白操作来拉动世界。
function themeTemplate_trim_excerpt( $content ) {
// split the content by spaces,
// remove excess whitespace
// pull the first 52 items,
// glue back together...
return implode( \' \', array_slice( array_filter( explode( \' \', $content ) ), 0, 52 ) );
}
remove_filter( \'get_the_excerpt\', \'wp_trim_excerpt\' );
add_filter( \'get_the_excerpt\', \'template_trim_excerpt\' );
故障
// Convert the string to an array
$words = explode( \' \', $content );
// Remove any empty values
$words = array_filter($words);
// Grab the first 10 elements from the array
$first_x_words = array_slice($words, 0, 10);
// Convert the array to a string
$output = implode ( \' \', $first_x_words );
// Final value
echo $output;
由于这些操作作用于空格,如果内容中有HTML,则可以将每个属性视为一个单词使用起来容易多了
wp_trim_words()
- [
L2951 ]