从Woocommerce面包屑中删除“Shop”

问题描述 投票:1回答:3

我没有主要的商店页面,只有产品类别。 Woocommerce面包屑总是在我需要删除的面包屑中显示“Shop”路径。在Woo文档中,我只能提供关于如何更改“home”slug或delimiter的信息,或者如何完全删除breadcrumb。我如何简单地删除“商店”路径?

编辑:我不想改变/更改“商店”路径的名称/链接,但完全删除它!

php wordpress woocommerce breadcrumbs
3个回答
4
投票

要从Woocommerce面包屑中完全删除“Shop”,请使用以下命令:

add_filter( 'woocommerce_get_breadcrumb', 'remove_shop_crumb', 20, 2 );
function remove_shop_crumb( $crumbs, $breadcrumb ){
    foreach( $crumbs as $key => $crumb ){
        if( $crumb[0] === __('Shop', 'Woocommerce') ) {
            unset($crumbs[$key]);
        }
    }

    return $crumbs;
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。


1
投票

为了完全控制breadcrubs输出,我建议复制文件breadcrumb.php位于 - > plugins / woocommerce / global / breadcrumb.php将它放在you-theme-folder / woocommerce / global / breadcrumb.php

我的默认面包屑看起来像这样:“主页»商店»主页»类别»子类别»产品”出于某种原因出现的家庭出现了两次。下面是breadcrumb.php的代码,它显示了我如何删除了“Home”和“Shop”的第一个表现

if ( ! empty( $breadcrumb ) ) {

echo $wrap_before;

foreach ( $breadcrumb as $key => $crumb ) {

    echo $before;

    //Every crumb have a $key which starts at 0 for the first crumb. 
    //Here I simply skip out of the loop for the first two crumbs. 
    //You can just echo the $key to see what number you need to remove. 
    if( $key === 0 || $key === 1 ){
        continue;
    }

    if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
        echo '<a href="' . esc_url( $crumb[1] ) . '">' . esc_html( $crumb[0] ) . '</a>';
    } else {
        echo esc_html( $crumb[0] );
    }

    echo $after;

    if ( sizeof( $breadcrumb ) !== $key + 1 ) {
        echo ' &raquo; ';
    }
}

echo $wrap_after;

}

要更改URL,只需在anchortag中为给定的$ key或crumb [0]值设置一个新值。如果您只想在商店的特定位置进行此操作,只需使用woocommerce条件函数,例如:

if(is_product()){
    if( $key === 0 || $key === 1 ){
       continue;
    }
}

仅在单个产品页面上删除两个第一个面包屑。在https://docs.woocommerce.com/document/conditional-tags/查看更多信息


0
投票
// Remove shop from breadcrumbs
function my_remove_shop_from_breadcrumbs( $trail ) {
   unset( $trail['shop'] );
   return $trail;
}
add_filter( 'wpex_breadcrumbs_trail', 'my_remove_shop_from_breadcrumbs', 20 );

在function.php中添加代码

© www.soinside.com 2019 - 2024. All rights reserved.