【问题标题】:How to remove product categories from breadcrumb on WooCommerce product page如何从 WooCommerce 产品页面上的面包屑中删除产品类别
【发布时间】:2021-03-03 16:50:24
【问题描述】:

如果有更好的方法可以做到这一点,请原谅我,因为我对这段代码不是很熟悉。我只想在面包屑上显示主页链接和当前产品。

想要的结果

目前

我找到了面包屑的代码,有没有办法只显示第一个和最后一个面包屑而不考虑层次结构?

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

    echo $before;

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

    echo $after;

    // Single product or Active title
    if(is_product() || flatsome_option('wc_category_page_title')){
            $key = $key+1;
            if ( sizeof( $breadcrumb ) > $key) {
                echo ' <span class="divider">'.$delimiter.'</span> ';
            }
    } else{
        
    // Category pages
    if ( sizeof( $breadcrumb ) !== $key + 1 ) {
            echo ' <span class="divider">'.$delimiter.'</span> ';
        }
    }

}

我这样做的原因是一些产品有多个类别,默认情况下,它只会显示主要类别的面包屑。我宁愿按照所有者的建议制作一个截断的版本。

我也想知道是否可以简单地动态检索产品标题和链接+静态主页链接,将其制作成简码,以便我可以将其粘贴到产品页面中。

【问题讨论】:

    标签: php wordpress woocommerce breadcrumbs


    【解决方案1】:

    嗨——上面答案中的第一个例子,也从 woocommerce 面包屑中删除了商店。这是一个工作示例,它只删除了类别:

    // remove only the category from woocommerce breadcrumbs
    add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
    function custom_breadcrumb( $crumbs, $breadcrumb ) {
    //print the array and look for the key with the category
    //echo '<pre>';
    //print_r($crumbs);
    //echo '</pre>';
    //unset($crumbs[2]); in my case it is key 2
    
    // only on the single product page
    if ( ! is_product() ) {
        return $crumbs;
    } else {
    
      unset($crumbs[2]); // this isn't enough, it would leave a trailing delimiter
      $newBreadC = array_values($crumbs); //therefore create new array
    
      return $newBreadC; //return the new array
    }
    }
    

    【讨论】:

      【解决方案2】:

      如果您想在产品页面上从产品面包屑中删除类别和子类别,您可以使用woocommerce_get_breadcrumb 挂钩。

      // change the breadcrumb on the product page
      add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
      function custom_breadcrumb( $crumbs, $breadcrumb ) {
      
          // only on the single product page
          if ( ! is_product() ) {
              return $crumbs;
          }
          
          // gets the first element of the array "$crumbs"
          $new_crumbs[] = reset( $crumbs );
          // gets the last element of the array "$crumbs"
          $new_crumbs[] = end( $crumbs );
      
          return $new_crumbs;
      
      }
      

      代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。

      一个不错的选择是为每个产品设置主要产品类别。您可以通过安装Yoast SEO 插件来做到这一点。

      您可以使用_yoast_wpseo_primary_product_cat 产品元设置主要产品类别的ID

      通过在 后端或导入 .csv 文件您只需更改 永久链接基于主要产品类别的面包屑

      更新产品永久链接

      // update the product permalink based on the primary product category
      add_filter( 'wc_product_post_type_link_product_cat', 'change_product_permalink_by_cat', 10, 3 );
      function change_product_permalink_by_cat( $term, $terms, $post ) {
      
          // get the primary term as saved by Yoast
          $primary_cat_id = get_post_meta( $post->ID, '_yoast_wpseo_primary_product_cat', true );
      
          // if there is a primary and it's not currently chosen as primary
          if ( $primary_cat_id && $term->term_id != $primary_cat_id ) {
              // find the primary term in the term list
              foreach ( $terms as $term_key => $term_object ) {
                  if ( $term_object->term_id == $primary_cat_id ) {
                      // return this as the primary term
                      $term = $terms[ $term_key ];
                      break;
                  }
              }
          }
      
          return $term;
      }
      

      更新产品页面上的产品面包屑

      // change the breadcrumb on the product page
      add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
      function custom_breadcrumb( $crumbs, $breadcrumb ) {
      
          // only on the single product page
          if ( ! is_product() ) {
              return $crumbs;
          }
      
          global $product;
          $new_crumbs = array();
      
          if ( $product->get_meta( '_yoast_wpseo_primary_product_cat', true ) ) {
              // gets the first element of the array "$crumbs"
              $new_crumbs[] = reset( $crumbs );
              // gets the id of the primary product category
              $primary_cat_id = $product->get_meta( '_yoast_wpseo_primary_product_cat', true );
              // create an array with all parent categories (based on the id of the primary product category)
              $parent_categories = get_ancestors( $primary_cat_id, 'product_cat' );
              $parent_categories = array_reverse($parent_categories);
              $parent_categories[] = $primary_cat_id;
              // for each product category it gets the name and the permalink
              foreach ( $parent_categories as $cat_id ) {
                  $term = get_term_by( 'id', $cat_id, 'product_cat' );
                  $new_crumbs[] = array(
                      0  =>  $term->name,
                      1  =>  esc_url( get_term_link( $term, 'product_cat' ) )
                  );
              }
              // gets the last element of the array "$crumbs"
              $new_crumbs[] = end( $crumbs );
          } else {
              // gets the first element of the array "$crumbs"
              $new_crumbs[] = reset( $crumbs );
              // gets the last element of the array "$crumbs"
              $new_crumbs[] = end( $crumbs );
          }
      
          return $new_crumbs;
      
      }
      

      代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。

      【讨论】:

      • 非常感谢文森佐!这解决了我的问题,也从你那里学到了一些新东西!
      猜你喜欢
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2018-07-07
      • 2016-11-22
      相关资源
      最近更新 更多