6种处理WordPress文章摘要的方法

本文针对WordPress摘要系统功能复杂、命名相似的问题,提出六种实用修改方法:通过excerpt_length调整字数、excerpt_more修改结尾文本、get_the_excerpt替换内容、wp_trim_excerpt生成段落或字符数限定摘要,以及wp_trim_words截取任意文本。所有方法均通过PHP代码在主题function.php中实现,无需改动其他文件。

文章作者:
阅读时间: 19 分钟
更新时间:2026年3月27日

WordPress的文章摘要系统效果很好,但它也感觉比它需要的更复杂。特别是在使用国外主题的过程中,往往不能很好的自动截断成为文章摘要。而其中主要问题(在WordPress中经常是这样)是WordPress摘要系统使用了许多具有接近完全相同的名称的函数。 这使得我们很难知道什么是什么,以及哪些功能可以帮助我们完成。

因此,本文不是试图深入研究创建WordPress摘要本身所涉及的每一项功能,而是根据我认为是人们使用或修改文章摘要的共同目标,对WordPress摘要采取“操作方法”的方法。

本文章将包含六个要求的相关详细说明:

  • 如何使用excerpt_length钩子更改WordPress摘要的长度(单词) – the_excerpt()和get_the_excerpt()使用的长度。
  • 如何使用excerpt_more钩子更改WordPress摘要末尾的[…]或“Read More”文本。
  • 如何使用get_the_excerpt钩子更改文章摘要的文本。
  • 如何使用wp_trim_excerpt钩子创建长度恰好为一段的摘录。
  • 如何使用wp_trim_excerpt钩子创建特定字符长度(而不是字长)的摘要。
  • 如何使用wp_trim_words()函数从任何字符串中获取任何长度的摘要。

顺便说一句,接下来这些示例都是WordPress的主要编程语言PHP。 他们广泛使用WordPress的钩子系统,尤其是WordPress filters。通过下面的代码,你只需要把它们添加到你的主题的funtion.php文件里即可。无需修改主题其他文件。

使用excerpt_length更改摘要长度

function iesay_longer_excerpts( $length ) {
  // Don't change anything inside /wp-admin/
  if ( is_admin() ) {
    return $length;
  }
  // Set excerpt length to 140 words
  return 140;
}
// "999" priority makes this run last of all the functions hooked to this filter, meaning it overrides them
add_filter( 'excerpt_length', 'iesay_longer_excerpts', 999 );

使用excerpt_more更改摘要“阅读更多”文本

function iesay_change_and_link_excerpt( $more ) {
  if ( is_admin() ) {
    return $more;
  }
  // Change text, make it link, and return change
  return '&hellip; <a href="' . get_the_permalink() . '">More »</a>';
 }
 add_filter( 'excerpt_more', 'iesay_change_and_link_excerpt', 999 );

使用get_the_excerpt更改文章摘要文本

function iesay_make_excerpt_text_interesting( $excerpt ) {
  if ( is_admin() ) {
    return $excerpt;
  }
  $excerpt = str_replace( array('rain', 'wind', 'scanty flame of the lamps'), 'DINOSAURS', $excerpt );
  return $excerpt;
}
add_filter( 'get_the_excerpt', 'iesay_make_excerpt_text_interesting', 999 );

使用wp_trim_excerpt创建首个段落摘要

function iesay_excerpt( $text ) {
  if( is_admin() ) {
    return $text;
  }
  // Fetch the content with filters applied to get <p> tags
  $content = apply_filters( 'the_content', get_the_content() );
  
  // Stop after the first </p> tag
  $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
  return $text;
}
// Leave priority at default of 10 to allow further filtering
add_filter( 'wp_trim_excerpt', 'iesay_excerpt', 10, 1 );

使用wp_trim_excerpt创建特定字符长度的摘录

function iesay_twitter_length_excerpt( $text ) {
  if( is_admin() ) {
    return $text;
  }
  // Fetch the post content directly
  $text = get_the_content();
  // Clear out shortcodes
  $text = strip_shortcodes( $text );
  
  // Get the first 140 characteres
  $text = substr( $text, 0, 140 );
  // Add a read more tag
  $text .= '…';
  return $text;
}
// Leave priority at default of 10 to allow further filtering
add_filter( 'wp_trim_excerpt', 'iesay_twitter_length_excerpt', 10, 1 );

使用wp_trim_words()从任意文本中获取任何长度摘要

// We're creating $read_more, a string that we'll place after the excerpt
$read_more = '&hellip; <a class="read-more-link" href="' . get_the_permalink() . '">Read Full Article</a>';
// wpautop() auto-wraps text in paragraphs
echo wpautop( 
  // wp_trim_words() gets the first X words from a text string
  wp_trim_words(
    get_the_content(), // We'll use the post's content as our text string
    55, // We want the first 55 words
    $read_more // This is what comes after the first 55 words
  )
);

关于wp_trim_words()函数的注释

wp_trim_words()是我们所看到的最基本的功能。它的实际行为是采用字符串 – 任何字符串 – 并从中返回第一个字,但是“Read More”字符串粘在最后。

您可以直接调用此函数来生成自己的WordPress摘要,完全在WordPress的the_excerpts()系统之外。虽然上面的示例仍然使用文章内容(使用get_the_content()引入),但您可以使用wp_trim_words()来生成任何内容的摘要 – 文章标题,元描述,作者简介,您可以命名它 – 因为wp_trim_words()可以接受任何内容文本字符串作为其第一个参数。

这篇文章有用吗?

点击星号为它评分!

平均评分 5 / 5. 投票数: 1

到目前为止还没有投票!成为第一位评论此文章。

在AI工具中继续讨论:
曾凤祥
曾凤祥
WordPress技术负责人
WordPress 独立站开发领域 10+ 年实践经验,长期专注于外贸独立站搭建与 SEO 优化,累计服务企业客户数百家(含制造业、外贸企业、政府等行业)
相关文章
无论你是否已有网站,我们都能帮你把线上业务推上新高度
无论什么行业,都能快速拥有专业网站:
无论什么行业,都能快速拥有专业网站:

展示型官网 / 品牌站 / 外贸独立站,均有成熟模板与定制方案
无需懂代码:可视化编辑+我们指导,轻松启动 → 快速上线,抢占先机​
结构清晰、利于SEO与后期运营,降低长期维护成本

立即查看建站方案
网站加载慢、跳出高、询盘少?
网站加载慢、跳出高、询盘少?

老旧体验与技术隐患会直接拖累获客与转化。
我们提供:网站全面诊断 → 速度/安全/结构优化 → 可持续运维支持(技术+策略),让网站真正成为您的业务增长工具,而不只是“线上门面”。

马上获取专属优化方案
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部