the_excerpt是WordPress内置函数中使用频率较高的函数。该函数主要作用是用来获取当前文章摘要并以[...]结尾。
如果作者在编辑文章时没有添加文章摘要,系统则会默认截取文章的前55个字的内容,默认截取内容会过滤HTML标签和图形,并且必须要在循环内使用。
//显示文章摘要 the_excerpt //使用方法 <?php the_excerpt() ?>
如果想截取文章标题字数,或是文章摘要字数,可以查看上一篇文章:
// 控制摘要字数 functiоn new_excerpt_length($length) { return 150; } add_filter(\"excerpt_length\", \"new_excerpt_length\");
return 150 是返回的字符数量,两个字符一个汉字,这个可以根据自己的需要进修改字数。
还可以修改摘要末尾的默认显示样式:
functiоn new_excerpt_more($excerpt) { return str_replace(\"[...]\", \"...\", $excerpt); } add_filter(\"wp_trim_excerpt\", \"new_excerpt_more\");
the_excerpt() 函数默认是以[...]结尾的,我们可以利用 php 的替换函数str_replace 将其替换成 ...,也可以改成你自己想要的符号。
添加自定义结尾:
functiоn new_excerpt_more($more) { global $post; return \" <a href=\"\" rel=\"external nofollow\" . get_permalink($post->ID) . \"\">阅读更多</a>\"; } add_filter(\"excerpt_more\", \"new_excerpt_more\");
上面代码会在文章摘要的结尾添加一个 阅读更多 的链接。
上面的代码按需要添加到主题 functiоns.php 文件中即可,注意,the_excerpt() 函数只能在循环内使用,否则的话会出现错误。
the_excerpt 函数位于wp-includes/post-template.php文件中。
标签:
本文链接:https://www.xiaba.cc/html/2046.html
版权声明:站内所有文章皆来自网络转载,只供模板演示使用,并无任何其它意义!