gpt4 book ai didi

php - WooCommerce - 从面包屑中删除产品标题但保留所有类别超链接

转载 作者:行者123 更新时间:2023-12-04 09:08:32 28 4
gpt4 key购买 nike

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

add_filter( 'woocommerce_get_breadcrumb', 'ed_change_breadcrumb' );

function ed_change_breadcrumb( $breadcrumb ) {

if(is_singular()){
array_pop($breadcrumb);
}

return $breadcrumb;
}

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

例如:

  • 原始面包屑

    <a>首页 </a>/<a>类别 </a>/<a>子类 </a>/产品名称

  • 上述函数的结果

    <a>首页 </a>/<a>类别 </a>/子类别

我需要子类别在从面包屑中删除产品标题后仍然可以点击。

谢谢

最佳答案

您的代码有效,但面包屑中的最后一个元素从不包含通过 global/breadcrumb.php 中使用的代码的链接在线模板文件 34

  • 可以通过将其复制到 yourtheme/woocommerce/global/breadcrumb.php 来覆盖此模板。

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

备注: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;

}

关于php - WooCommerce - 从面包屑中删除产品标题但保留所有类别超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63401763/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com