【问题标题】:Remove "Shop" from BreadCrumb in WooCommerce从 WooCommerce 中的 BreadCrumb 中删除“Shop”
【发布时间】:2017-09-04 09:23:45
【问题描述】:

如何在 woo commerce 中从面包屑中删除 Shop 文本?[Home-->Shop-->Pink Himalayan Salt] 我想根据我在 m WordPress 网站中的导航菜单设置面包屑。[主页-->产品-->盐-->粉红喜马拉雅盐] 我在我的主菜单中使用了一些页面、自定义链接、类别和产品。

看截图。

Bredcrumb -


菜单 -

【问题讨论】:

  • 您是否使用任何插件来做到这一点?
  • 据我所知,我的朋友是不可能的。您需要调用菜单而不是面包屑,这是唯一的方法。
  • @Pratikbhatt 我只使用 Woocommerce 和 TheGem 主题默认插件。
  • 您可以在面包屑脚本中添加条件if(!is_shop())
  • @HarshSigma 我有可能是你的解决方案……如果你能给我一些反馈,谢谢。

标签: php wordpress woocommerce breadcrumbs


【解决方案1】:

您可以通过主题覆盖 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. 将“商店”替换为“产品”
  2. 当“商店”不退出时,在“主页”之后添加“产品”。

因此,您的面包屑将始终以:Home &gt; Products 在商店、档案和单个产品页面上...

【讨论】:

    【解决方案2】:

    这可以通过 CSS 解决,但除非您发布指向您商店的链接,否则无济于事。 试试这样:

    将此行添加到您的自定义 CSS 中

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

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

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

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

    【讨论】:

    • 感谢回答。但我不只是想删除商店。我也想替换动态面包屑。
    【解决方案3】:

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

    复制文件: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;
    
    }
    

    【讨论】:

      【解决方案4】:

      我通过对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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        • 2013-05-19
        • 2016-01-03
        相关资源
        最近更新 更多