WooCommerce类别和子类别的固定链接

时间:2018-01-14 作者:Anton Flärd

安装程序

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 没有运气我做错了什么,或者我可以重新布线什么部分才能让它工作?非常适合任何提示、线索或答案。感谢您阅读本文!

1 个回复
最合适的回答,由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\');

结束

相关推荐

How to display CPT archives?

我有一个网站,我们合并了两个博客,一个在普通博客中,另一个在自定义帖子类型中。一切都很好,只是我们无法为CPT显示月度档案。我们尝试了各种各样的插件,无论我们得到什么样的常规博客帖子列表,我们都会被重定向到那里。我们启用了has\\u存档,那么我们缺少什么呢?我从未尝试合并两个博客,所以这有点困难。但在普通博客上,第一个归档文件也显示了26篇文章,但当你访问它时,什么都没有显示。其他月份可以吗?Problem BlogRegular Blog