wordpress WooCommerce-如何从网址中删除产品和产品类别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43447175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WooCommerce- How to remove product & product-category from urls?
提问by Bhagya Shree
I'm using WooCommerce on a WordPress and it adds product & product-category to the
我在 WordPress 上使用 WooCommerce,它将产品和产品类别添加到
URLs.
网址。
http://dev.unwaveringmedia.com/8dim/product-category/all-party-supplies/http://dev.unwaveringmedia.com/8dim/product/14-snowman-serving-tray/
http://dev.unwaveringmedia.com/8dim/product-category/all-party-supplies/ http://dev.unwaveringmedia.com/8dim/product/14-snowman-serving-tray/
I need to remove 'product' & 'product-category' from the URLs. Is there any way to modify the permalinks and remove them?
我需要从 URL 中删除“产品”和“产品类别”。有没有办法修改永久链接并删除它们?
回答by Shehroz Altaf
Yes. But Please read this article first https://docs.woocommerce.com/document/removing-product-product-category-or-shop-from-the-urls/
是的。但请先阅读这篇文章https://docs.woocommerce.com/document/removing-product-product-category-or-shop-from-the-urls/
You can change this by:
您可以通过以下方式更改:
you can change the permalinks in Settings > permalink > optional > Product category base= ./ (type ./ in Product category base).
Be sure that you don't have any page, post or attachment with the same name (slug) as the category page or they will collide and the code won't work.
Install and activate the plugin below: (For more info please see https://timersys.com/remove-product-category-slug-woocommerce/)
您可以在“设置”>“永久链接”>“可选”>“产品类别基础= ./”中更改永久链接(在“产品类别基础”中键入 ./)。
确保您没有任何与类别页面同名(slug)的页面、帖子或附件,否则它们会发生冲突并且代码将不起作用。
安装并激活以下插件:(有关更多信息,请参阅https://timersys.com/remove-product-category-slug-woocommerce/)
`
`
<?php
/*
Plugin Name: Remove product-category slug
Plugin URI: https://timersys.com/
Description: Check if url slug matches a woocommerce product category and use it instead
Version: 0.1
Author: Timersys
License: GPLv2 or later
*/
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;
});`
For more info please see https://timersys.com/remove-product-category-slug-woocommerce/
有关更多信息,请参阅https://timersys.com/remove-product-category-slug-woocommerce/
回答by Manu Garrido
Using Shehroz Altaf's trick does the job just (almost) perfectly.
使用 Shehroz Altaf 的技巧(几乎)完美地完成了这项工作。
Adding the following between the $slug
and $exists
declarations will make it work with sub-categories too.
在$slug
和$exists
声明之间添加以下内容也将使其适用于子类别。
$slug_array = explode( '/', $slug );
$slug = array_values(array_slice($slug_array, -1))[0];
回答by Alex
It wasn't working with pagination for me. Here is full code that's working.
它不适用于我的分页。这是有效的完整代码。
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;
});
This will add new rewrite rule that will paginate query.
这将添加新的重写规则,将分页查询。
add_action('init', 'do_rewrite');
function do_rewrite(){
add_rewrite_rule( '[^/]+/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&paged=$matches[2]', 'top' );
add_filter( 'query_vars', function( $vars ){
$vars[] = 'attachment';
$vars[] = 'paged';
return $vars;
} );}
回答by fightstarr20
Other solutions all failed for me, after some trial and error I came up with this solution...
其他解决方案对我来说都失败了,经过反复试验,我想出了这个解决方案......
- Install free 'WOO CATEGORY BASE PERMALINK FIXER' plugin from https://masterns-studio.com/code-factory/wordpress-plugin/woo-category-base/
- Set Product Category Base Permalinks to : shop
- Set Custom Base Permalinks to : /shop/%product_cat%/
- 从https://masterns-studio.com/code-factory/wordpress-plugin/woo-category-base/安装免费的“WOO CATEGORY BASE PERMALINK FIXER”插件
- 将产品类别基础固定链接设置为:shop
- 将自定义基本固定链接设置为:/shop/%product_cat%/
URLs will then look like
URL 将看起来像
http://www.example.com/shop/category/sub-category/product
Seems to work fine for pagination and sub categories
似乎适用于分页和子类别