Skip to content

Avada 使用 Yoast 面包屑导航去掉 Portfolio

  • by

Avada 自带了面包屑导航功能。

如果你同时也安装了 Yoast SEO 插件,它也带有面包屑导航功能,到 Settings -> Advanced -> Breadcrumbs 中打开最下面的

【Enable breadcrumbs for your theme】

就会替代 Avada 自带的面包屑导航,SEO 自带的面包屑导航功能往往包含结构化数据,更好。

问题是:

1,面包屑导航中间不显示 Portfolio 分类了

2,面包屑 Home 后多了一个 “Portfolio” 的归档页

其实不止是对于 Portfolio 这样,添加了其它 Custom Post Type 也会这样。

不止是对于 Yoast SEO 这样,Rank Math 也是这样。

问题 1 很容易解决,只需要对 Custom Post Type 选择主分类即可,因为有多个分类法的话要选择哪个显示在面包屑之中。

对于 Avada 和 Yoast 在 Settings -> Advanced -> Breadcrumbs -> Breadcrumbs for post types 中为 Portfolio (avada_portfolio)选择 Portfolio Categories

对于问题 2 需要添加代码

add_filter('wpseo_breadcrumb_links', 'remove_custom_post_type_archive_from_breadcrumbs');

function remove_custom_post_type_archive_from_breadcrumbs($links) {
    // Loop through the breadcrumb links array
    foreach ($links as $index => $link) {
        // Check if this is the custom post type archive link
        if ($link['text'] === 'Portfolio') {
            // Remove the custom post type archive link from the breadcrumb trail
            unset($links[$index]);
            break; // Exit the loop once the link is found and removed
        }
    }

    return $links;
}

Rank Math 也是类似

add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    // 获取当前页面的对象
    $queried_object = get_queried_object();

    // 检查是否为 product 自定义类型的页面
    if ( is_singular('avada_portfolio') ) {
        // 遍历面包屑数组
        foreach ( $crumbs as $key => $crumb ) {
            // 如果找到上级项为 "Products" 的元素,移除它
            if ( isset( $crumb[0] ) && $crumb[0] === 'Portfolio' ) {
                // 移除上级项
                unset( $crumbs[ $key ] );
                // 重建数组索引
                $crumbs = array_values( $crumbs );
                break;  // 结束遍历,因为我们已经找到并移除了目标元素
            }
        }
    }
    return $crumbs;
}, 10, 2);

那如果使用了 Page 来制作 Custom taxonomy 分类页,面包屑上链接的确是自带归档页,该怎么办呢?除了 301 有没有更好的办法?

Leave a Reply

Your email address will not be published. Required fields are marked *