可以使用 functions.php
中的以下代码片段控制 WP 帖子的默认(自动)摘录长度;
来自WP Codex
// . Post excerpt adjustment (Auto)
// . ==============================
function wpdocs_custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
My question is how do you limit the manual one?
You know, the exerpt specifically added by the user themselves?
(*) 有一个 8 年前的问题 here ,这确实提供了一些背景信息,但考虑到今年和 WP 取得的进展,我想再次发布这个问题并获得关于这个主题的一些澄清。
添加上下文:(编辑:2019 年 3 月 12 日)
这并不是说之前发布的问题的原始答案不起作用,尽管看起来真的很笨拙。我正在使用 exerpt_length
过滤器寻找更简单和可靠的答案。而不是使用类似下面的东西来修剪文本; (如果可能)
function excerpt($limit) {
return wp_trim_words(get_the_excerpt(), $limit);
}
最佳答案
我们默认在核心中有以下过滤:
add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
但在 wp_trim_excerpt()
中,当没有手动摘录集时,修剪仅应用于帖子的内容。
这是一个未经测试的自定义过滤建议:
add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
if ( has_excerpt( $post ) ) {
$excerpt_length = apply_filters( 'excerpt_length', 55 );
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
}
return $excerpt;
}, 10, 2 );
对手动摘录应用类似的修剪。
希望您可以根据自己的需要进一步调整。
关于php - 如何限制 WP 中的手动摘录长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55080012/