安装程序
Desired permalink structure (only these structures exist for products)
Product 1: /shop/category/subcategory/product1/
Product 2: /shop/category/product2/
Woocommerce Product View Settings
View for shopbase: Show categories
View for categories: Show products
Products
Products are only checked for one category OR subcategory
Permalink Settings
Normal settings: /%category%/%postname%/
Base for categories: shop
For products: /shop/%product_cat%/
Archives
Since there are over 100 subcategories, I want to use an archive to list all
the products.
通过以下尝试,我已经能够生成一个工作
Product-page
但在这样做的同时
404:ing
子类别页面。我记不起哪个选项会产生什么结果。
我为产品类别库尝试了不同的permalink设置,包括%category%/%product_cat%
, %category%/%product_cat%/%postname%
, %product_cat%/%product_cat%
, shop/%product_cat%
, shop/%product_cat%/%postname%
, 以及以上所有结尾为/.
此外,我还尝试使用base shop permalink设置替换上述内容我尝试更改产品视图设置以显示子类别而不是产品我已经尝试在有工作产品页面的情况下为子类别创建页面,但子类别页面仍然返回404
.我试过了this SO attempt 没有运气我做错了什么,或者我可以重新布线什么部分才能让它工作?非常适合任何提示、线索或答案。感谢您阅读本文!
最合适的回答,由SO网友:Anton Flärd 整理而成
我能够用以下代码解决这个问题,这些代码为每个子类别生成rewrite\\u规则,这在匹配过程中是首选的,因为它更具体:
function wpse_291143_generate_taxonomy_rewrite_rules( $wp_rewrite ) {
global $wp_rewrite;
$base = "shop";
$rules = array();
$terms = get_terms( array( \'taxonomy\' => \'product_cat\', \'hide_empty\' => false ));
foreach($terms as $term) {
$term_children = get_terms( array(
\'taxonomy\' => \'product_cat\',
\'parent\' => intval($term->term_id),
\'hide_empty\' => false
)
);
if($term_children) {
foreach ( $term_children as $term_child ) {
$rules[$base . \'/\' . $term->slug . \'/\' . $term_child->slug . \'/?$\'] = \'index.php?product_cat=\' . $term_child->slug;
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
add_action(\'generate_rewrite_rules\', \'wpse_291143_generate_taxonomy_rewrite_rules\');