WordPress获取文章摘要函数the_excerpt详解

the_excerpt是wordpress内置函数中使用频率较高的函数。该函数主要作用是用来获取当前文章摘要并以[…]结尾。

如果作者在编辑文章时没有添加文章摘要,系统则会默认截取文章的前55个字的内容,默认截取内容会过滤HTML标签和图形,并且必须要在循环内使用。文章摘要英文

函数描述

//显示文章摘要
the_excerpt
//使用方法
<?php the_excerpt() ?>

如果想截取文章标题字数,或是文章摘要字数,可以查看上一篇文章:

《 WordPress如何截取文章摘要/文章标题/文章内容字数》

函数使用

// 控制摘要字数
function new_excerpt_length($length) {
return 150;
}
add_filter("excerpt_length", "new_excerpt_length");

r文章摘要是什么意思eturn 150 是返回的字符数量,两个字符一个汉字,这个可以根据自己的需要进修改字数。

还可以修改摘要末尾的默认显示样式:

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

the_excerpt() 函数默认是以[…]结尾的,我们可以利用 p文章摘要模板范文hp 的替换函数str_replace 将其替换成 …,也可以改成你自己想要的符文章摘要的作用号。

添加自定义结尾:

function new_excerpt_more($more) {
global $post;
return " <a href="" rel="external nofollow" . get_permalink($post->ID) . "">阅读更多</a>";
}
add_filter("excerpt_more", "new_excerpt_more");

上面代码会在文章摘要的结尾添加一个 阅读更多 的链接。

上面的代码按需要添加到主题 fu文章摘要怎么提炼nctions.php 文件中即可,注意,the_excerpt() 函数只能在循环内使用,否则的话会出现错误。

函数原型

the_excerpt 函数位于wp-includes/post-template.php文件中。

© 版权声明
THE END
喜欢就支持一下吧
点赞8赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容