You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为Understrap WordPress主题添加自定义excerpt_length

为Understrap主题设置50长度的摘要(excerpt_length)集成方案

WordPress默认摘要长度为55个单词,通过excerpt_length过滤器可自定义该数值——这个过滤器接收一个整数参数,返回修改后的长度值。

具体实现步骤

  1. 添加自定义摘要长度的过滤函数
    将以下代码添加到你的代码文件中:
// 自定义Understrap主题的摘要长度为50个单词
add_filter( 'excerpt_length', 'custom_understrap_excerpt_length', 999 );

if ( ! function_exists( 'custom_understrap_excerpt_length' ) ) {
    function custom_understrap_excerpt_length( $length ) {
        // 返回想要的摘要单词数
        return 50;
    }
}
  1. 与现有摘要链接代码共存
    你提供的understrap_all_excerpts_get_more_link函数负责给摘要添加"Read More"链接,上述代码和这个函数完全兼容,两者可放在同一文件中。

代码放置建议

  • 优先推荐:子主题的functions.php
    将代码添加到子主题的functions.php文件中,这样Understrap主题更新时,你的自定义设置不会被覆盖。
  • 若修改父主题(不推荐):
    可将代码添加到inc/extras.php文件里,和现有的摘要链接函数放在一起,但主题更新后改动会丢失。

注意事项

  • 设置优先级为999,确保这个设置能覆盖主题或其他插件可能设置的摘要长度。
  • 如果在文章编辑后台手动填写了自定义摘要,这个长度设置不会生效,手动摘要会直接使用你输入的内容,不会被截断。

内容的提问来源于stack exchange,提问作者McHunkerson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 03:01:38