WordPress摘要有几种方法(附:WordPress摘要的完整代码)
文 / @WordPress主题
一、WordPress中有几种方法可以使用代码来实现摘要:
- 使用
the_excerpt()
函数:这个函数可以将文章的摘要显示在页面上。您可以在主题的模板文件中使用它来显示文章摘要,例如在博客布局中的列表中显示。 - 使用
get_the_excerpt()
函数:这个函数与the_excerpt()
类似,但它会返回文章的摘要,而不是将其显示在页面上。您可以使用这个函数在页面上的其他位置显示文章的摘要。 - 使用文章的自定义字段:您可以在文章的自定义字段中设置摘要。然后,可以使用
get_post_meta()
函数获取文章的自定义字段值并将其
二、WordPress摘要是在WordPress博客中显示在文章列表页面的简短摘要,可以帮助读者了解文章内容。下面是完整的代码:
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function new_excerpt_more( $more ) {
return '...';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
function get_the_custom_excerpt( $length ) {
add_filter( 'excerpt_length', function () use ( $length ) {
return $length;
}, 999 );
$output = get_the_excerpt();
$output = apply_filters( 'wptexturize', $output );
$output = apply_filters( 'convert_chars', $output );
return $output;
}
// 使用方法:
echo get_the_custom_excerpt(20);
第一个函数custom_excerpt_length用来自定义摘要的长度,第二个函数new_excerpt_more用来自定义摘要末尾的省略号,第三个函数get_the_custom_excerpt用来获取文章的摘要,其中传入的参数$length就是摘要的长度

相关文章
-
搭建一个WordPress网站需要多少成本 2023-11-06 00:09:51
-
Symlink介绍(附:如何使用Symlink进行WordPress开发) 2023-11-05 23:38:32
-
让WordPress实现数据库同步的插件:HyperDB 2023-10-24 23:40:49
-
allegro电商平台值得做吗(附:2023年Allegro注册流程指南) 2023-10-08 21:53:39
-
印度跨境电商平台有哪些(附:印度跨境电商做什么产品好) 2023-10-08 21:34:23
-
跨境电商必看的几大海外二手电商平台 2023-10-08 18:04:42
-
WordPress同城互联网产品解决方案:UBASE 2023-10-03 16:40:39
-
WordPress网站的安全插件:wordfence 2023-09-14 09:25:18
-
WordPress 6.3 支持在手动更新插件和主题失败后回滚旧版本 2023-08-28 16:58:45
-
WordPress 6.3版本(2023年8月)性能提升了哪些? 2023-08-28 16:56:02