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

如何在WordPress中移除Yoast面包屑Schema指定ListItem?

解决Yoast SEO面包屑Schema中移除指定ListItem的问题

问题原因

你之前的代码无效,核心原因是wpseo_schema_graph_pieces过滤器处理的是顶级Schema节点,而ListItem并不是独立的顶级节点——它是嵌套在Breadcrumb节点的itemListElement数组里的子元素。直接过滤ListItem实例会找不到目标元素,自然无法移除。

正确解决方案

我们需要定位到Breadcrumb类型的Schema节点,修改其内部的itemListElement数组,移除指定列表项,同时修正剩余项的position值(避免移除后出现position断层)。

将以下代码添加到主题的functions.php文件中:

add_filter( 'wpseo_schema_graph_pieces', 'remove_specific_breadcrumb_list_item', 11, 2 );

function remove_specific_breadcrumb_list_item( $pieces, $context ) {
    // 遍历所有Schema节点,定位Breadcrumb类型节点
    foreach ( $pieces as $key => $piece ) {
        if ( $piece instanceof \Yoast\WP\SEO\Generators\Schema\Breadcrumb ) {
            // 获取当前面包屑的列表元素数组
            $list_elements = $piece->data['itemListElement'];
            
            // 过滤掉name为"Producten"的ListItem
            $filtered_elements = array_filter( $list_elements, function( $item ) {
                return $item['name'] !== 'Producten';
            } );
            
            // 重新排序剩余项的position值,保证连续递增
            $filtered_elements = array_values( $filtered_elements );
            foreach ( $filtered_elements as $index => &$element ) {
                $element['position'] = $index + 1;
            }
            
            // 更新Breadcrumb节点的列表元素
            $pieces[$key]->data['itemListElement'] = $filtered_elements;
            break; // 假设页面只有一个面包屑节点,找到后退出循环
        }
    }
    
    return $pieces;
}

自定义过滤规则

如果你需要根据position(比如移除position=2的项)来过滤,只需把过滤条件替换为:

return $item['position'] !== 2;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 23:05:37