WordPress 原创插件 国人习惯、简约实用、容易上手【点击了解】
方法一:the_excerpt()函数
wordpress 内置函数the_excerpt()是个使用频率较高的函数,它是用来获取当前文章摘要的,以[...]结尾,如果在文章中没有编辑内容摘要字段,则默认截取文章的前55个字的内容,默认截取的字段去掉HTML标签和图形,并且一定要在循环内使用。
这个标签没有任何的参数,直接使用即可。但有时候我们会不习惯用[...]结尾,或者感觉 55 个字符太少。这就需要我们来调整下functions.php文件,重新定义下函数了。
控制摘要的字数:
/*控制摘要字数*/
function new_excerpt_length($length) {
return 150;
}
add_filter("excerpt_length", "new_excerpt_length");
添加自定义结尾:
function new_excerpt_more($more) {
global $post;
return " <a href="". get_permalink($post->ID) . "">阅读更多</a>";
}
add_filter("excerpt_more", "new_excerpt_more");
更改摘要末尾的默认显示样式:
function new_excerpt_more($excerpt) {
return str_replace("[...]", "...", $excerpt);
}
add_filter("wp_trim_excerpt", "new_excerpt_more");
方法二:wp_trim_words()函数
<?php
echo wp_trim_words( get_the_content(), 100 ); // 文章内容,限制100个字符,溢出用...
echo wp_trim_words( get_the_excerpt(), 20 ); // 文章摘要,限制20个字符
echo wp_trim_words( get_the_title(), 10 ); // 文章标题,限制10个字符
?>