如果你的文章需要摘要信息可以使用此函数,使用此函数需要了解如下四点1、它自动提取文章内容前55个字符作为文章摘要2、摘要不含有任何html和图片标签3、此函数只能用在循环中4、文章没有明确提供摘要信息
函数用法
the_excerpt();
往往使用默认的是无法满足我们的需求的,因此需要对此改造。
修改摘要的长度
默认是55个字符,我们可以使用excerpt_length过滤器钩子改变摘要(excerpt)的长度,代码如下:
function new_excerpt_length($length) {
return 150;
}
add_filter(‘excerpt_length’, ‘new_excerpt_length’);
修改摘要末尾的显示
摘要末尾附加的字符串默认设置为[…],修改末尾的显示可以用excerpt_more过滤器钩子,代码如下:
function new_excerpt_more($more) {
return ‘…’;
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);
末尾加阅读更多
function new_excerpt_more($more) {
global $post;
return ‘…[阅读更多]’;
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);
将以上代码加入到function.php中即可。