首页 文章

通过主题自定义Woocommerce中的产品类别面包屑链接

提问于
浏览
1

我正在尝试修改 class-wc-breadcrumb.php 以自定义产品页面的痕迹中的产品类别链接 .

此文件位于: wp-content/plugins/woocommerce/includes

我尝试将我的子主题中的此文件复制并编辑为:
wp-content/themes/Divi-child/woocommerce/includes/lass-wc-breadcrumb.php
But it doesn't work.

如何通过我的孩子主题自定义Woocommerce中的产品类别面包屑链接?

2 回答

  • 2

    无法通过您的主题更改核心文件,并且出于多种原因严禁执行此操作 .

    相反,你有两个选择:

    • 使用任何可用的操作和过滤钩子来进行WooCommerce面包屑自定义 .

    • 自定义WooCommerce模板 global/breadcrumb.php via your active child theme

    你案子中最好的选择 is the 1st option . 在下面的代码中,您将能够自定义Woocommerce产品类别链接的每个链接 .

    在下面的代码中,我使用foreach循环遍历每个 $crumb . 每个 $crumb 都是一个数组,其中:

    • $crumb[0] 是显示的标签名称

    • $crumb[1] 是将使用 $crumbs[$key][1] 更改的链接(或网址)...

    在允许自定义 $crumbs[$key][1] 中的链接之前,我们将检查当前 $crumb[0] 是否为产品类别名称 .

    您必须为每个产品类别设置新链接,将自己必要的代码添加到此过滤的钩子函数示例中 .

    代码:

    add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 10, 2 );
    function custom_breadcrumb( $crumbs, $object_class ){
        // Loop through all $crumb
        foreach( $crumbs as $key => $crumb ){
            $taxonomy = 'product_cat'; // The product category taxonomy
    
            // Check if it is a product category term
            $term_array = term_exists( $crumb[0], $taxonomy );
    
            // if it is a product category term
            if ( $term_array !== 0 && $term_array !== null ) {
    
                // Get the WP_Term instance object
                $term = get_term( $term_array['term_id'], $taxonomy );
    
                // HERE set your new link with a custom one
                $crumbs[$key][1] = home_url( '/'.$term->slug.'/' ); // or use all other dedicated functions
            }
        }
    
        return $crumbs;
    }
    

    代码位于活动子主题(或活动主题)的function.php文件中 .

    经过测试和工作 .

  • 0

    有很多选项可以更改选项..

    为了这

    add_filter( 'woocommerce_breadcrumb_defaults', 'jk_woocommerce_breadcrumbs' );
    function jk_woocommerce_breadcrumbs() {
    return array(
            'delimiter'   => ' / ',
            'wrap_before' => '<nav class="woocommerce-breadcrumb" itemprop="breadcrumb">',
            'wrap_after'  => '</nav>',
            'before'      => '',
            'after'       => '',
            'home'        => _x( 'Home', 'breadcrumb', 'woocommerce' ),
        );
    }
    

    您可以通过此更改所有内容 .

    欲了解更多信息: - https://docs.woocommerce.com/document/customise-the-woocommerce-breadcrumb/

相关问题