【问题标题】:Customizing product categories breadcrumb links in Woocommerce via the theme通过主题在 Woocommerce 中自定义产品类别面包屑链接
【发布时间】:2018-07-30 20:46:50
【问题描述】:

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

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

我尝试将这个文件在我的子主题中复制并编辑到:
wp-content/themes/Divi-child/woocommerce/includes/lass-wc-breadcrumb.php
但它不起作用。

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

【问题讨论】:

  • 只是将其复制到其他文件夹并期望它能够正常工作不是模板。你改变了什么部分? woocommerce 是一个非常多变的插件,你可以使用actions/hooks 来实现你的目标
  • 我尝试更改以下函数:私有函数 add_crumbs_single( $post_id = 0, $permalink = '' ) 目标是通过我正在显示的静态页面更改类别 Woocommerce 的所有链接产品

标签: php wordpress woocommerce breadcrumbs custom-taxonomy


【解决方案1】:

不可能通过您的主题更改核心文件,并且出于多种原因严格禁止这样做。

相反您有 2 个选项

  1. 使用任何可用的操作和过滤钩子进行 WooCommerce 面包屑自定义。
  2. 自定义 WooCommerce 模板 global/breadcrumb.php via your active child theme

在您的情况下,最佳选择是第一个选项。在下面的代码中,您将能够自定义 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文件中。

经过测试并且有效。

【讨论】:

  • 太棒了,这正是我所需要的。我将以下代码添加到函数中:
【解决方案2】:

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

为此

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/

【讨论】:

  • 谢谢,我读了这个页面,但是如果我更新了会发生什么?我的代码不会被删除吗?
  • 您需要在子主题中执行此操作,并且有“woocommerce_breadcrumb_defaults”作为过滤器,您可以根据需要进行修改
【解决方案3】:
add_filter( 'woocommerce_breadcrumb_defaults', 'ts_woocommerce_breadcrumbs_change' );
function ts_woocommerce_breadcrumbs_change() {
    return array(
            'delimiter'   => '  ',
            'wrap_before' => '<ul class="breadcrumb load-animate theme-animate fadeInUp animated" style="animation-delay: 0.3s;">',
            'wrap_after'  => '</ul>',
            'before'      => '<li> ',
            'after'       => '</li>',
            'home'        => _x( 'Home', 'breadcrumb', 'woocommerce' ),
            
        );
}

【讨论】:

    猜你喜欢
    • 2018-02-08
    • 2018-04-26
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多