摘录完整指南
我最近回答了一些关于摘录的问题,所以我将尽可能详细地解释一下。
HTML 标记/格式
the_excerpt() 首先没有任何参数,所以什么都不能传递给它。事实上,the_excerpt() 将内容修剪为 55 个单词,并且在返回文本之前删除所有 html 标记。 the_excerpt() 位于wp-includes/post-template.php。要在摘录中允许某些或所有 html 标记,必须创建一个新的摘录。
首先需要先把原来的函数去掉,再把新函数挂到get_the_excerpt上。请注意,这个新的摘录仍然可以在模板文件中调用为the_excerpt(),无需更改。 get_the_excerpt() 位于wp-includes/post-template.php。
摘录使用wp_trim_excerpt返回修剪后的文本,所以我们需要先从摘录过滤器中删除wp_trim_excerpt。 wp_trim_excerpt() 位于wp-includes/formatting.php,第 2355 行。方法如下:
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
您现在可以将新摘录添加到get_the_excerpt
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
要允许 html 标签/格式,我们需要指定您需要允许的标签。您可以使用以下strip_tags 语句来实现这一点
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags());
第二个参数wpse_allowedtags() 是一个小函数,用于添加the_excerpt() 允许的标签。有关有效 HTML5 标记的完整列表,请查看 here。这是函数,添加任何需要允许/保留的 html 标记
function wpse_allowedtags() {
// Add custom tags to this string
return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>';
}
如果您需要允许所有 HTML 标签,即不剥离任何标签,strips_tags() 函数可以完全省略/删除。
需要注意的是,当允许使用 html 标记时,这些标记被计为单词,因此您对带标记和不带标记的摘录的字数将不一样。要更正此问题,您需要先从实际字数中删除这些标签,以便只计算字数。
我写了一个摘录,它将允许所有标签,仅将单词计为单词,并在设定的单词数量之后完成一个句子(不会修剪句子中间的文本)并在最后一个单词之后添加阅读更多文本.
这是完整的代码
function wpse_allowedtags() {
// Add custom tags to this string
return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>';
}
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
global $post;
$raw_excerpt = $wpse_excerpt;
if ( '' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
//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', $wpse_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;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . ' » ' . sprintf(__( 'Read more about: %s »', 'wpse' ), get_the_title()) . '</a>';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
$wpse_excerpt .= $excerpt_end; /*Add read more in new paragraph */
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
你可以从你需要额外的函数中删除'//'。
自定义摘录长度
有时您需要显示不同长度的简单摘录,并且为每个帖子/功能/页面编写摘录是不可行的。这是使用wp_trim_words的一个不错的小函数
function wpse_custom_excerpts($limit) {
return wp_trim_words(get_the_excerpt(), $limit, '<a href="'. esc_url( get_permalink() ) . '">' . ' …' . __( 'Read more »', 'wpse' ) . '</a>');
}
这个小函数所做的是将get_the_excerpt 修整为用户设置的$limit,并在最后返回带有阅读更多链接的文本。
您可以在模板中按以下方式调用此摘录
echo wpse_custom_excerpts($limit);
$limit 将是您的字数,因此 30 字的摘录将是
echo wpse_custom_excerpts(30);
这里只需要记住一件事,如果您将限制设置为超过 55 个单词,则只会返回 55 个单词,因为摘录的长度只有 55 个单词。如果需要更长的摘录,请改用get_the_content。
自定义摘录长度
如果只需要改变the_excerpt()的长度,可以使用如下函数
function wpse_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'wpse_excerpt_length', 999 );
请记住,您需要将优先级设置为大于 10,以便您的自定义函数在默认值之后执行。
添加阅读链接
摘录返回的所有文本的末尾都带有讨厌的[...],无法点击。要在 hellips 的位置添加阅读更多文本,请使用此功能
function wpse_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __('Read More', 'your-text-domain') . '</a>';
}
add_filter( 'excerpt_more', 'wpse_excerpt_more' );
编辑
摘录第一段
我想保持完整,所以这是第一段之后的摘录
这是一个保持 HTML 标记完整的函数,在摘录末尾添加“阅读更多”链接,并在第一段之后修剪摘录。
if ( ! function_exists( 'wpse0001_custom_wp_trim_excerpt' ) ) :
function wpse0001_custom_wp_trim_excerpt($wpse0001_excerpt) {
global $post;
$raw_excerpt = $wpse0001_excerpt;
if ( '' == $wpse0001_excerpt ) {
$wpse0001_excerpt = get_the_content('');
$wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
$wpse0001_excerpt = apply_filters('the_content', $wpse0001_excerpt);
$wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, '</p>' ) + 4 );
$wpse0001_excerpt = str_replace(']]>', ']]>', $wpse0001_excerpt);
$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($wpse0001_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$wpse0001_excerpt .= $excerpt_end;
return $wpse0001_excerpt;
}
return apply_filters('wpse0001_custom_wp_trim_excerpt', $wpse0001_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse0001_custom_wp_trim_excerpt');