一聚教程网:一个值得你收藏的教程网站

热门教程

WordPress the_excerpt()函数文章摘要字数并加上链接

时间:2022-06-25 19:01:12 编辑:袖梨 来源:一聚教程网

WordPress里显示文章摘要的函数the_excerpt()默认是显示55个字,对于一些模板来说,只显示55个字的摘要貌似有些短,因此我们有必要在模板函数functions.php里面对the_excerpt()做个改造,使之按着我们的需求来展示更多(或更少)的摘要。

改动很简单,在functions.php里加上这么一段即可:

 代码如下 复制代码

function emtx_excerpt_length( $length ) {
 return 92; //把92改为你需要的字数,具体就看你的模板怎么显示了。
}
add_filter( 'excerpt_length', 'emtx_excerpt_length' );

上面的只是对数字控制了,那么要如何给字符带上链接地址呢

处理方法很简单,我们只要往主题的functions.php里加上这么一段代码

 代码如下 复制代码

function emtx_continue_reading_link() {
 return ' 查看全文→';
}

function emtx_auto_excerpt_more( $more ) {
 return ' …' . emtx_continue_reading_link();
}
add_filter( 'excerpt_more', 'emtx_auto_excerpt_more' );
 
function emtx_custom_excerpt_more( $output ) {
 if ( has_excerpt() && ! is_attachment() ) {
  $output .= emtx_continue_reading_link();
 }
 return $output;
}
add_filter( 'get_the_excerpt', 'emtx_custom_excerpt_more' );

热门栏目