如何在\\u摘录中包含换行符。我通过我的函数修改了长度和“更多”按钮。php。我在每一篇博客文章中都使用了挑逗,有时它看起来有点难看,因为没有包含换行符。
如何在摘录中包含换行符?
没有允许您将允许的标记设置为不被删除的筛选器the_excerpt()
. 可以说是核心的一个缺点。
无论如何,实际的摘录生成不会发生在该模板标记中,而是完全发生在其他地方:
摘录由函数生成wp_trim_excerpt()
, 其中包含您已经使用的摘录过滤器(excerpt_length
和excerpt_more
) 应用了哪些调用wp_trim_words()
, 这反过来又要求wp_strip_all_tags()
. 这三个函数都位于wp include/formatting中。php
因此,在没有案例过滤器的情况下,不可避免地会出现你的摘录wp_strip_all_tags()
, 保留某些标记的唯一可能性是为添加自定义替换函数wp_trim_excerpt()
:
function wpse67498_wp_trim_excerpt( $text = \'\' ) {
$raw_excerpt = $text;
if ( \'\' == $text ) {
$text = get_the_content( \'\' );
$text = strip_shortcodes( $text );
$text = apply_filters( \'the_content\', $text );
$text = str_replace( \']]>\', \']]>\', $text );
$excerpt_length = apply_filters( \'excerpt_length\', 55 );
$excerpt_more = apply_filters( \'excerpt_more\', \' \' . \'[...]\' );
$allowable = \'<br>\';
$text = preg_replace( \'@<(script|style)[^>]*?>.*?</\\\\1>@si\', \'\', $text );
$text = trim( strip_tags( $text, $allowable ) );
if ( \'characters\' == _x( \'words\', \'word count: words or characters?\' )
&& 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 ) > $excerpt_length ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $excerpt_more;
} else {
$text = implode( $sep, $words_array );
}
}
return apply_filters( \'wp_trim_excerpt\', $text, $raw_excerpt );
}
remove_filter( \'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter( \'get_the_excerpt\', \'wpse67498_wp_trim_excerpt\' );
和约翰内斯·皮勒的解决方案一样,咆哮解决方案应该更具适应性。
详细信息:
覆盖功能wp_trim_excerpt
- 以下是完整代码:
// append to themes/{your_theme}/functions.php
define(\'EXCERPT_RARELY\', \'{[}]\');
define(\'EXCERPT_BR\', nl2br(PHP_EOL));
function so306588_trim_excerpt_custom($text = \'\')
{
add_filter(\'the_content\', \'so306588_trim_excerpt_custom_mark\', 6);
// get through origin filter
$text = wp_trim_excerpt($text);
remove_filter(\'the_content\', \'so306588_trim_excerpt_custom_mark\', 6);
return so306588_trim_excerpt_custom_restore($text);
}
function so306588_trim_excerpt_custom_mark($text)
{
$text = nl2br($text);
return str_replace(EXCERPT_BR, EXCERPT_RARELY, $text);
}
function so306588_trim_excerpt_custom_restore($text)
{
return str_replace(EXCERPT_RARELY, EXCERPT_BR, $text);
}
// remove default filter
remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
// add custom filter
add_filter(\'get_the_excerpt\', \'so306588_trim_excerpt_custom\');
对摘录应用\\u内容过滤器不是更简单吗?
我只是偶尔在模板文件中使用此选项:
$excerpt = apply_filters(\'the_content\', ( get_post(get_the_ID())->post_excerpt) );
echo $excerpt;
目前,可以使用the_excerpt()
功能显示带换行符的摘录。但如果要返回结果,可以使用如下函数:
function get_the_excerpt_theme() {
$excerpt = get_the_excerpt();
$excerpt = apply_filters( \'the_excerpt\', $excerpt );
return $excerpt;
}
它对摘录应用过滤器,使用apply_filters(\'the_excerpt\', $excerpt )
类似于wp功能the_excerpt()
但返回字符串而不显示它。此外,如果您希望在摘录中只允许换行,可以添加$excerpt = strip_tags($excerpt,\'<br>\')
线下apply_filters
.
希望有帮助!
也可以只使用PHP的nl2br()函数:
echo nl2br(get_the_excerpt());
--或--nl2br(the_excerpt());