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

WordPress中修改摘要起始位置的问题求助

问题描述

每篇WordPress文章开头都带有<ul class="toc_post_list">标签的目录(Table of Contents),但调用文章摘要(excerpt)时,目录总会被包含在内。尝试编写自定义函数跳过该ul标签,提取其后第一个<p>标签内容作为摘要,但函数无法生效,尝试的代码如下:

// Function to modify the excerpt
function custom_excerpt($excerpt) {
global $post;

// Check if the current post has a manual excerpt set
if ($post->post_excerpt) {
    return $excerpt;
} else {
    // If no manual excerpt is set, get the content and find the first <p> tag
    $content = apply_filters('the_content', $post->post_content);
    $excerpt = '';

    // Exclude content inside <ul class="toc_post_list">
    $pattern = '/<ul\sclass="toc_post_list">(.*?)<\/ul>/s';
    $content_without_toc = preg_replace($pattern, '', $content);

    // Use regex to find the first <p> tag after excluding the specified content
    preg_match('/<p>(.*?)<\/p>/', $content_without_toc, $matches);

    if ($matches) {
        $excerpt = $matches[0];
    }

    return $excerpt;
 }
}

// Apply the custom excerpt function to the 'get_the_excerpt' and 'the_excerpt' filter hooks
add_filter('get_the_excerpt', 'custom_excerpt');
add_filter('the_excerpt', 'custom_excerpt');
问题原因
  1. 钩子选择不当:get_the_excerpt和the_excerpt的执行时机晚于WordPress默认的摘要生成逻辑,自定义处理会被后续的默认过滤(比如自动截断、添加省略号)覆盖。
  2. 内容过滤干扰:apply_filters('the_content', $post->post_content)会触发一系列内容过滤(比如自动格式化段落、转义特殊字符),导致HTML结构被修改,正则匹配失效。
  3. 正则匹配HTML的局限性:正则无法完美处理HTML的嵌套结构,若ul标签内嵌套其他标签,或者class属性使用单引号,都会导致匹配失败。
  4. 全局变量不可靠:在部分上下文(如循环外),全局$post变量可能指向错误的文章,导致获取内容错误。
修正方案

改用wp_trim_excerpt钩子(WordPress生成自动摘要的核心钩子),并使用DOMDocument解析HTML替代正则,代码如下:

function custom_excerpt($excerpt, $post_id) {
    // 获取当前文章对象
    $post = get_post($post_id);
    
    // 存在手动摘要则直接返回
    if (!empty($post->post_excerpt)) {
        return $excerpt;
    }

    // 获取原始文章内容,避免the_content过滤干扰
    $content = $post->post_content;

    // 初始化DOM解析器,忽略HTML格式警告
    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->loadHTML('<?xml encoding="UTF-8">' . $content);
    libxml_clear_errors();

    $xpath = new DOMXPath($dom);
    // 定位并移除目标目录标签
    $tocNodes = $xpath->query('//ul[@class="toc_post_list"]');
    if ($tocNodes->length > 0) {
        $tocNodes->item(0)->parentNode->removeChild($tocNodes->item(0));
    }

    // 查找移除目录后的第一个p标签
    $pNodes = $xpath->query('//p[1]');
    if ($pNodes->length > 0) {
        $excerpt = $dom->saveHTML($pNodes->item(0));
        // 若需要仅保留文本内容,可取消下面注释
        // $excerpt = strip_tags($excerpt);
    } else {
        // 无p标签时,使用默认摘要逻辑
        $excerpt = wp_trim_words(strip_tags($content), 55);
    }

    return $excerpt;
}
// 挂钩wp_trim_excerpt,传递两个参数
add_filter('wp_trim_excerpt', 'custom_excerpt', 10, 2);
修正说明
  • 钩子优化:wp_trim_excerpt是生成自动摘要的核心钩子,优先级更高,能确保自定义逻辑先于默认处理执行。
  • DOM解析替代正则:DOMDocument能精准处理HTML结构,避免正则匹配的各种边界问题。
  • 原始内容处理:直接使用$post->post_content,绕过the_content过滤,保证HTML结构原始性。
  • 容错机制:无p标签时自动回退到默认摘要逻辑,避免返回空内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 08:25:04