WordPress摘要有几种方法(附:WordPress摘要的完整代码)

文 / @WordPress主题

20230116084904884

一、WordPress中有几种方法可以使用代码来实现摘要:

  1. 使用 the_excerpt() 函数:这个函数可以将文章的摘要显示在页面上。您可以在主题的模板文件中使用它来显示文章摘要,例如在博客布局中的列表中显示。
  2. 使用 get_the_excerpt() 函数:这个函数与 the_excerpt() 类似,但它会返回文章的摘要,而不是将其显示在页面上。您可以使用这个函数在页面上的其他位置显示文章的摘要。
  3. 使用文章的自定义字段:您可以在文章的自定义字段中设置摘要。然后,可以使用 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就是摘要的长度

添加UTHEME为好友
扫码添加UTHEME微信为好友
· 分享WordPress相关技术文章,主题上新与优惠动态早知道。
· 微信端最大WordPress社群,限时免费入群。