WooCommerce-从面包屑中删除产品标题,但保留所有类别的超链接 [英] WooCommerce - Remove product title from breadcrumbs but keep all category hyperlinks

查看:117
本文介绍了WooCommerce-从面包屑中删除产品标题,但保留所有类别的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下功能从产品页面上显示的面包屑中删除产品标题:

I'm using the following function to remove the product title from the breadcrumbs displayed on the product page:

add_filter( 'woocommerce_get_breadcrumb', 'ed_change_breadcrumb' );

function ed_change_breadcrumb( $breadcrumb ) {
    
  if(is_singular()){
        array_pop($breadcrumb);
    }
  
  return $breadcrumb;
}

它的工作方式是确实删除了标题,但是它也阻止了最后一个类别/子类别成为超链接.我该如何解决?

It works in that it does remove the title, but it also stops the last category/sub-category from being a hyperlink. How can I fix that?

例如:

  • 原始面包屑

  • Original breadcrumb

< a> Home </a> /< a> Category </a> /< a> 子类别</a> /产品标题

<a>Home</a> / <a>Category</a> / <a>Sub Category</a> / Product Title

上述功能的结果

< a> Home </a> /< a> Category </a> /子类别

<a>Home</a> / <a>Category</a> / Sub Category

从面包屑中删除产品标题后,我需要子类别"仍可单击.

I need the Sub Category to still be clickable after removing the product title from the breadcrumbs.

谢谢

推荐答案

您的代码可以工作,但面包屑中的最后一个元素从不包含通过

Your code works but the last element in the breadcrumbs never contains a link through the code used in global/breadcrumb.php template file on line 34

  • 可以通过将该模板复制到 yourtheme/woocommerce/global/breadcrumb.php 来覆盖该模板.
  • This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.

因此,您可以删除过滤器挂钩,并在模板文件中应用以下代码,以便在 is_product()为true时,它提供到最后一个元素的链接

So you can remove your filter hook and apply the following code in the template file so that it provides a link to the last element when is_product() is true

注意: is_product() -返回在单个产品页面上为true. is_singular()

替换

if ( ! empty( $breadcrumb ) ) {

    echo $wrap_before;

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

        echo $before;

        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 $delimiter;
        }
    }

    echo $wrap_after;

}

使用

if ( ! empty( $breadcrumb ) ) {

    echo $wrap_before;

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

        echo $before;

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

        echo $after;

        if ( sizeof( $breadcrumb ) !== $key + 1 ) {
            if ( is_product() && sizeof( $breadcrumb ) == $key + 2 ) {
                echo '';
            } else {
                echo $delimiter;
            }
        }
    }

    echo $wrap_after;

}

这篇关于WooCommerce-从面包屑中删除产品标题,但保留所有类别的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆