各位同学,今天咱们来聊聊WordPress里一个挺有意思的小家伙——the_excerpt_more
过滤器。简单来说,它就是控制摘要(excerpt)末尾那个“阅读更多”链接的幕后推手。
开场白:摘要的诞生与“阅读更多”的使命
在WordPress的世界里,摘要就像一部电影的预告片,它用简短精炼的文字,吸引读者点击进入正片(完整文章)。而“阅读更多”链接,就是那张通往正片的电影票。
默认情况下,WordPress会自动生成文章的摘要,并会在摘要末尾加上一个“阅读更多”链接。但默认的链接可能不够个性化,比如你可能想修改链接文字,或者改变链接的跳转方式。这时候,the_excerpt_more
过滤器就派上用场了。
第一幕:the_excerpt_more
过滤器的真面目
the_excerpt_more
过滤器允许你自定义文章摘要的“阅读更多”链接。它接收一个参数,即默认的“阅读更多”链接文本(通常是[...]
),然后你可以通过修改这个参数,来达到自定义链接的目的。
这个过滤器位于wp-includes/formatting.php
文件中,在wp_trim_excerpt()
函数中被调用。wp_trim_excerpt()
函数负责生成摘要,如果文章没有手动摘要,它就会自动截取文章内容的前面一部分作为摘要。
第二幕:源码剖析,揭秘运作原理
让我们深入到源码中,看看the_excerpt_more
是如何工作的:
function wp_trim_excerpt( $text = '' ) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content( '' );
$text = strip_shortcodes( $text );
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text );
$text = str_replace( ']]>', ']]>', $text );
/**
* Filters the number of words used in an excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
/**
* Filters the string used to more fully indicate an excerpt was cut off.
*
* @since 2.9.0
*
* @param string $more_string The excerpt string.
*/
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
/**
* Filters the trimmed excerpt string.
*
* @since 2.8.0
*
* @param string $text The trimmed text.
* @param string $raw_excerpt The text prior to trimming.
*/
return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
}
注意看这段代码:
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
这里,apply_filters()
函数将 'excerpt_more'
过滤器应用到 ' ' . '[…]'
这个字符串上。' ' . '[…]'
就是默认的“阅读更多”链接文本,它包含一个空格和一个省略号。这意味着,你可以通过添加一个函数到 excerpt_more
过滤器,来改变这个默认文本。
wp_trim_words()
函数使用 $excerpt_more
变量的值来添加 "阅读更多" 文本到截断的文章摘要中。
第三幕:实战演练,自定义“阅读更多”链接
现在,让我们通过几个例子,来演示如何使用the_excerpt_more
过滤器:
例1:修改链接文字
假设你想把默认的 […]
修改为 (继续阅读)
。你可以在你的主题的 functions.php
文件中添加以下代码:
function my_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">(继续阅读)</a>';
}
add_filter( 'excerpt_more', 'my_excerpt_more' );
这段代码定义了一个名为 my_excerpt_more
的函数,它接收 $more
参数(默认的“阅读更多”链接文本),并返回一个新的链接文本。然后,add_filter()
函数将 my_excerpt_more
函数添加到 excerpt_more
过滤器中。
注意: 这里我们使用了 get_permalink( get_the_ID() )
函数来获取当前文章的链接。get_the_ID()
函数返回当前文章的 ID,get_permalink()
函数根据文章 ID 返回文章的链接。
例2:移除“阅读更多”链接
如果你想完全移除“阅读更多”链接,你可以返回一个空字符串:
function my_excerpt_more( $more ) {
return '';
}
add_filter( 'excerpt_more', 'my_excerpt_more' );
例3:添加CSS类
如果你想给“阅读更多”链接添加一个CSS类,以便你可以通过CSS来控制链接的样式,你可以这样做:
function my_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">阅读更多</a>';
}
add_filter( 'excerpt_more', 'my_excerpt_more' );
在这个例子中,我们给链接添加了一个 read-more
类。你可以在你的主题的 style.css
文件中添加相应的CSS样式,来控制链接的显示效果。
例4:使用条件判断
有时候,你可能想根据不同的条件,显示不同的“阅读更多”链接。比如,你可能想在文章的摘要中显示“阅读更多”链接,但在页面的摘要中不显示。你可以这样做:
function my_excerpt_more( $more ) {
if ( is_page() ) {
return ''; // 如果是页面,则移除“阅读更多”链接
} else {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">阅读更多</a>'; // 如果是文章,则显示“阅读更多”链接
}
}
add_filter( 'excerpt_more', 'my_excerpt_more' );
在这个例子中,我们使用了 is_page()
函数来判断当前页面是否是页面。如果是页面,我们就返回一个空字符串,移除“阅读更多”链接。如果是文章,我们就显示“阅读更多”链接。
例5:结合 the_content_more_link
过滤器
WordPress还有一个过滤器叫做 the_content_more_link
,它用来控制文章内容中的“阅读更多”链接(通过 <!--more-->
标签插入的)。the_excerpt_more
控制的是摘要中的,而 the_content_more_link
控制的是文章正文的。
如果你想统一控制摘要和文章正文中的“阅读更多”链接,你可以同时使用这两个过滤器。
function my_custom_more_link( $more_link, $more_text ) {
return '<a href="' . get_permalink() . '" class="more-link">' . $more_text . '</a>';
}
add_filter( 'the_content_more_link', 'my_custom_more_link', 10, 2 );
function my_excerpt_more( $more ) {
global $post;
return '...<a href="' . get_permalink( $post->ID ) . '" class="read-more">阅读更多</a>';
}
add_filter( 'excerpt_more', 'my_excerpt_more' );
第四幕:进阶技巧,玩转摘要生成
除了修改“阅读更多”链接,你还可以通过其他过滤器来控制摘要的生成。
excerpt_length
过滤器: 控制摘要的长度(单词数)。wp_trim_excerpt
过滤器: 允许你完全自定义摘要的内容,包括“阅读更多”链接。
让我们看一个例子:
function my_excerpt_length( $length ) {
return 20; // 修改摘要长度为20个单词
}
add_filter( 'excerpt_length', 'my_excerpt_length' );
function my_custom_excerpt( $text, $raw_excerpt ) {
if ( '' == $raw_excerpt ) {
$text = substr( get_the_content(), 0, 200 ) . '... <a href="'. get_permalink() . '">继续阅读</a>';
}
return $text;
}
add_filter( 'wp_trim_excerpt', 'my_custom_excerpt', 10, 2 );
在这个例子中,我们首先使用 excerpt_length
过滤器将摘要长度修改为20个单词。然后,我们使用 wp_trim_excerpt
过滤器完全自定义摘要的内容。如果文章没有手动摘要,我们就截取文章内容的前200个字符,并添加一个“继续阅读”链接。
第五幕:注意事项,避免踩坑
- 位置: 一定要把你的代码添加到主题的
functions.php
文件中,或者添加到自定义插件中。不要直接修改WordPress的核心文件,因为升级WordPress会导致你的修改丢失。 - 优先级:
add_filter()
函数的第三个参数是优先级。优先级越低,函数执行的越早。默认的优先级是10。如果你想让你的函数在其他函数之前执行,你可以设置一个更低的优先级。 - 参数: 确保你的函数接收了正确的参数。
the_excerpt_more
过滤器接收一个参数($more
),wp_trim_excerpt
过滤器接收两个参数($text
和$raw_excerpt
)。
总结:the_excerpt_more
的价值与意义
the_excerpt_more
过滤器虽然只是一个小小的工具,但它却能让你更好地控制你的WordPress网站的摘要显示,从而提升用户体验。通过自定义“阅读更多”链接,你可以让你的网站更具个性化,更能吸引读者。
希望今天的讲座对你有所帮助。如果你还有任何问题,欢迎随时提问。 祝大家编程愉快!