网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   WordPress (https://www.0058.net/forumdisplay.php?f=68)
-   -   wordpress 更改excerpt的长度,设置excerpt后面'[...]'的字样和链接 (https://www.0058.net/showthread.php?t=5175)

Janessa 2015-06-14 06:19 AM

wordpress 更改excerpt的长度,设置excerpt后面'[...]'的字样和链接
 
1、更改系统默认excerpt的长度

默认情况下,excerpt长度设置为55个字,可以使用excerpt_length过滤器改变excerpt的长度,添加如下代码到主题的functions.php文件即可。

function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');

2、更改系统默认设置更多样式

excerpt后面更多字符串默认设置为'[...]'。为了改变excerpt的更多字符串形式使用excerpt_more过滤器,添加如下代码在主题的functions.php文件即可。

function new_excerpt_more($more) {
return '......';
}
add_filter('excerpt_more', 'new_excerpt_more');

对于2.9以上版本使用如下代码:

function new_excerpt_more($excerpt) {
return str_replace('[...]', '...', $excerpt);
}
add_filter('wp_trim_excerpt', 'new_excerpt_more');

3、给文章"read more"添加链接

给文章"read more"添加链接,将如下代码放到主题的functions.php文件。

function new_excerpt_more($more) {

global $post;

return '<a href="'.get_permalink($post->ID). '">Read the Rest...</a>';
}

add_filter('excerpt_more', 'new_excerpt_more');


源文件:wp_trim_excerpt()在wp-includes/formatting.php.


All times are GMT +8. The time now is 03:44 AM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.