繁体   English   中英

woocommerce 中的类别和产品的相同基础 url

[英]Same base url for categories and products in woocommerce

woocommerce 中的类别和产品是否可以具有相同的基础 url? 目前它为我的类别页面提供了 404,但产品运行良好。

我想做的是两者都有基本的“商店”类别:demo.com/shop/category/ 产品:demo.com/shop/category/product-name

解决方案可能是Woocommerce 的永久链接

尝试将其添加到您的函数中。php

 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');

我尝试使用此解决方案,但类别页面仍然转到 404。我已将产品类别基础设置为“商店”,并将产品类别基础设置为“/shop/%product_cat%/”我的店铺基础是“/店铺'。 我做错了什么?

您可以查看此答案https://stackoverflow.com/a/73333483/6835106

add_filter( 'rewrite_rules_array', function( $rules )
{
    $new_rules = array(
        'shop/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]',
        'shop/([^/]*?)/?$' => 'index.php?product_cat=$matches[1]',
    );
    return $new_rules + $rules;
} );

将此代码添加到functions.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM