WordPress修改the_excerpt()函数摘要末尾默认省略号[…]

WordPress教程 1962

the_excerpt() 函数默认将 “[…]” 作为摘要末尾的更多内容字符显示,如果想更换为更多内容超链接,或者不想要方括号 “[]” 只显示省略号 “…”,可以通过 wordpress 钩子 excerpt_more 来修改,且很简单。

在主题的 functions.php 文件,添加下面函数即可

1、去掉方括号

1
2
3
4
function theme_excerpt_more($more) {
    return '...';
}
add_filter('excerpt_more', 'theme_excerpt_more');

2、修改为更多超链接

1
2
3
4
5
function theme_excerpt_more($more) {
    global $post;    
    return '<a href="'.get_permalink($post->ID). '" title="阅读全文">更多...</a>';
}
add_filter('excerpt_more', 'theme_excerpt_more');

附:excerpt_more 钩子由 wp_trim_excerpt() 函数调用

文件位置:wp-includes/formatting.php

WordPress官方文档:https://developer.wordpress.org/reference/hooks/excerpt_more/

精品推荐: