View Single Post
  #1   IP: 112.87.85.4
Old 2015-06-14, 06:19 AM
Janessa Janessa is offline
初级会员
 
Join Date: 2006-07-12
Posts: 2
Janessa 现在声名狼藉
Default 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.
Reply With Quote