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

如何让两个移除Yoast Schema的函数协同生效并支持指定任意页面?

解决Yoast Schema移除函数冲突问题

问题根源

你写的两个函数都挂载了wpseo_json_ld_output过滤器,但存在两个关键问题:

  1. 函数仅在匹配指定ID时返回false,不匹配时无返回值(默认返回null),导致过滤器链后续处理异常。
  2. 同优先级的两个过滤器链式执行时,前一个函数返回的null会覆盖原始输出,让后一个函数无法正确判断。

修复方案

把两个函数的逻辑合并成一个,同时确保不满足条件时返回原始输出参数,保证过滤器链正常运行。以下是优化后的代码:

// 移除指定文章/页面的Yoast Schema
add_filter( 'wpseo_json_ld_output', 'yoast_seo_remove_schema_for_specific_posts', 10, 1 );

function yoast_seo_remove_schema_for_specific_posts( $output ) {
    // 在这里添加所有需要移除Schema的内容ID(支持文章、页面、自定义文章类型)
    $target_post_ids = [ 2544, 2743, 15 ];
    
    // 判断当前是否为单篇内容,且ID在目标列表中
    if ( is_singular() && in_array( get_the_ID(), $target_post_ids ) ) {
        return false; // 返回false移除Schema
    }
    
    // 不满足条件时返回原始输出,确保其他内容的Schema正常生成
    return $output;
}

代码说明

  • is_singular():检测当前页面是否为单篇内容(文章、页面、自定义文章类型都适用)。
  • $target_post_ids:统一管理需要移除Schema的内容ID,后续要添加新的页面/文章,直接往数组里加ID即可。
  • 必须返回$output:当不需要移除Schema时,把原始输出返回,保证其他页面的Yoast Schema正常工作。

扩展用法

如果需要针对不同内容类型单独设置(比如只移除文章里的某些ID,或只移除页面里的某些ID),可以修改判断逻辑:

function yoast_seo_remove_schema_for_specific_posts( $output ) {
    // 文章ID列表
    $post_ids = [ 2544, 2743 ];
    // 页面ID列表
    $page_ids = [ 15 ];
    
    if ( ( is_single() && in_array( get_the_ID(), $post_ids ) ) || ( is_page() && in_array( get_the_ID(), $page_ids ) ) ) {
        return false;
    }
    
    return $output;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 03:20:17