删除" Shop"来自WooCommerce中的BreadCrumb

时间:2017-09-04 09:23:46

标签: php wordpress woocommerce breadcrumbs

如何从woo commerce中的面包屑中删除购物文字?[主页 - >商店 - > Pink Himalayan Salt] 我想根据我在WordPress网站上的导航菜单设置面包屑。[主页 - >产品 - >盐 - >粉红色喜马拉雅盐] 我使用了一些页面,自定义链接,类别&产品到我的主菜单。

见截图。

Bredcrumb - enter image description here

菜单 - https://www.screencast.com/t/To2xchRWJo

4 个答案:

答案 0 :(得分:2)

您可以通过主题覆盖WooCommerce模板(阅读以下官方文档)

Template Structure + Overriding Templates via a Theme

plugins/woocommerce/templates/global/breadcrumb.php 中复制文件后 要: themes/yourtheme/woocommerce/global/breadcrumb.php ,您可以通过将其替换为以下内容来更改代码:

<?php
/**
 * Shop breadcrumb
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.3.0
 * @see         woocommerce_breadcrumb()
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! empty( $breadcrumb ) ) {

    $breadcrumb0 = $breadcrumb[0];
    $shop_txt = __( 'Shop', 'woocommerce' );
    $products_txt = __( 'Products', 'woocommerce' );
    $products_url = home_url( '/products/' );
    $breadcrumb10 = array( $products_txt );
    $breadcrumb11 = array( $products_txt, $products_url );
    if(is_product() || is_shop() || is_product_category() || is_product_tag() ){
        if( $breadcrumb[1][0] == $shop_txt ){
            if( ! empty( $breadcrumb[1][1] ) )
                $breadcrumb[1] = $breadcrumb11;
            else
                $breadcrumb[1] = $breadcrumb10;
        } else {
            unset($breadcrumb[0]);
            array_unshift($breadcrumb, $breadcrumb0, $breadcrumb11);
        }
    }

    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;

}

这将:

  1. 替换&#34;购物&#34; by&#34; Products&#34;
  2. 添加&#34;产品&#34;就在&#34; Home&#34;当&#34; Shop&#34;不会退出。
  3.   

    因此,您的面包将始终以商店,档案和单品页面上的 Home > Products 开头......

答案 1 :(得分:0)

这可以通过CSS解决,但除非您发布到商店的链接,否则无法解决。 试试这样:

将此行添加到自定义CSS

ul.breadcrumbs li:nth-of-type(2) {display:none}

如果它不起作用,可能还需要!important

ul.breadcrumbs li:nth-of-type(2) {display:none!important}

我无法评论这就是我必须回答的原因。请提供您网站的链接。我会用精确的CSS更新我的答案。

答案 2 :(得分:0)

我通过对functions.php进行更改得到了答案 https://www.screencast.com/t/U42lqPduY707

if (get_post_type() ==  'product')
{
echo sprintf($link, '#', esc_html__('Products', 'thegem'));
//echo sprintf($link, get_permalink(get_option ('woocommerce_shop_page_id' , 0 )), esc_html__('Product', 'thegem'));
$taxonomy = 'product_cat';
$terms = get_the_terms( $post->ID, $taxonomy );
foreach ( $terms as $c ) {
$c->term_id;
//  echo '<a href="' . get_term_link($c, 'product_cat') . '">' . ($c->name ) . '</a>';
if($c->term_id=='36') {
echo $delimiter;
echo sprintf($link, get_permalink( 106 ), esc_html__($c->name, 'thegem'));
}
}
}
else {
$slug = $post_type->rewrite;
printf($link, $home_link . '/' . $slug['slug'] . '/', $post_type->labels->singular_name);
}

答案 3 :(得分:0)

受LoicTheAztec的启发,这是解决类似但略有不同要求的解决方案。假设您只是想完全删除“商店”链接:

复制文件: plugins / woocommerce / templates / global / breadcrumb.php

至: themes / yourtheme / woocommerce / global / breadcrumb.php

在新文件中,查找行

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

在该行之后,添加以下行:

if (trim(strip_tags($crumb[0])) == 'Shop') { continue; }

所以最终代码将如下所示:

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! empty( $breadcrumb ) ) {

    echo $wrap_before;

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

if (trim(strip_tags($crumb[0])) == 'Shop') { continue; }

        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;

}