如何从woocommerce 类目链接中去掉 Product-Category

比如要把这个类目链接: https://www.fobwebs.com/product-category/apple/
改成: https://www.fobwebs.com/apple/

需要分2步操作:

1. 在你的WP主题 functions.php 中添加如下代码:

add_filter('request', function( $vars ) {
   global $wpdb;
   if( ! empty( $vars['pagename'] ) || ! empty( $vars['category_name'] ) || ! empty( $vars['name'] ) || ! empty( $vars['attachment'] ) ) {
      $slug = ! empty( $vars['pagename'] ) ? $vars['pagename'] : ( ! empty( $vars['name'] ) ? $vars['name'] : ( !empty( $vars['category_name'] ) ? $vars['category_name'] : $vars['attachment'] ) );
      $exists = $wpdb->get_var( $wpdb->prepare( "SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = 'product_cat' AND t.slug = %s" ,array( $slug )));
      if( $exists ){
         $old_vars = $vars;
         $vars = array('product_cat' => $slug );
         if ( !empty( $old_vars['paged'] ) || !empty( $old_vars['page'] ) )
            $vars['paged'] = ! empty( $old_vars['paged'] ) ? $old_vars['paged'] : $old_vars['page'];
         if ( !empty( $old_vars['orderby'] ) )
            $vars['orderby'] = $old_vars['orderby'];
         if ( !empty( $old_vars['order'] ) )
            $vars['order'] = $old_vars['order']; 
      }
   }
   return $vars;
});

2. 在后台 settings>permalinks> product category base 里,输入小数点, 如下图:

如何从woocommerce 类目链接中去掉 Product-Category插图

不过, WOOCOMMERCE 官方有篇文章指出,不建议这么做!

文章链接: https://docs.woocommerce.com/document/removing-product-product-category-or-shop-from-the-urls/

没有账号? 忘记密码?

社交账号快速登录