要获得更好的方法,您可以使用get_the_excerpt
过滤器:
function get_excerpt($excerpt="",$limit=140){
$excerpt = preg_replace(" (\\[.*?\\])",\'\',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = mb_substr($excerpt, 0, $limit);
$excerpt = mb_substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( \'/\\s+/\', \' \', $excerpt));
$excerpt = $excerpt.\'...\';
return $excerpt;
}
add_filter(\'get_the_excerpt\',"get_excerpt");
更改
$limit=140
设置为所需的字符数。如果您想以不同的方式:
add_filter(\'get_the_excerpt\',function ($excerpt="",$limit=140){
$excerpt = preg_replace(" (\\[.*?\\])",\'\',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = mb_substr($excerpt, 0, $limit);
$excerpt = mb_substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( \'/\\s+/\', \' \', $excerpt));
$excerpt = $excerpt.\'...\';
return $excerpt;
});
这将避免任何与现有函数名类似的冲突
get_excerpt
.