WordPress摘要有几种方法(附:WordPress摘要的完整代码)
文 / @UTHEME
一、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插件:All in One WP Security & Firewall 怎么样(附:AIOWPS拦截垃圾评论设置教程) 2023-01-26 21:51:51
-
在 PHP 中如何通过一行代码就交换两个变量的值 2023-01-26 00:18:31
-
WordPress主题:Pin Premium (WordPress仿Pinterest样式瀑布流主题) 2023-01-25 23:36:43
-
免费WordPress主题:Maupassant(WordPress中文博客主题) 2023-01-25 23:31:29
-
WordPress响应式图片(Responsive Image)功能关闭:取消WordPress图片宽带和高度参数 2023-01-24 10:49:32
-
wordpress报错:修复MySQL 1064错误问题 错误原因和解决方法 2023-01-24 10:42:04
-
WordPress 实现静态化搜索结果页面的方法 2023-01-24 00:41:44
-
WP Rocket – 强大的WordPress缓存加速优化插件(附:WP Rocket插件设置过程与七折优惠码) 2023-01-24 00:41:44
-
get_permalink、get_the_permalink和the_permalink有啥区别? 2023-01-24 00:41:08
-
为什么选用WordPress搭建独立站? 2023-01-23 20:25:21