我们如何更改特定产品类别的 woocommerce 单一产品别名?

问题描述 投票:0回答:1

我正在尝试更改特定类别的 woocommerce 单一产品 slug,就像我们默认的那样

home_url/产品/样品产品

我想将此父级 slug 更改为其他内容,但针对特定类别的产品。

home_url/trips/sample-product(如果是特定产品类别->“trips”)

使用 rewrite_rules 尝试这些代码行

add_action('generate_rewrite_rules', 'custom_product_rewrite_rules');

   function custom_product_rewrite_rules($wp_rewrite) {
       // Add new rewrite rule to match the custom permalink structure
       $wp_rewrite->rules = array(
           '^trips/([^/]*)/?$' => 'index.php?product=$matches[1]',
             // Rewrite rule for the new permalink structure
           '^trips/([^/]*)-([0-9]+)/?$' => 'index.php?product=$matches[2]',
             // Rewrite rule for product permalinks with IDs
           '^([^/]*)-([0-9]+)/?$' => 'index.php?p=$matches[2]', 
             // Assuming this rule is still needed
           '^([^/]*)/([^/]*)-([0-9]+)/?$' => 'index.php?product_cat=$matches[1]&product=$matches[3]'
       ) + $wp_rewrite->rules;
   }

我们还有更好的选择吗?

php wordpress woocommerce hook-woocommerce
1个回答
0
投票

我使用此过滤器挂钩修改特定类别(旅行)中产品的永久链接结构。

add_filter('post_type_link', 'custom_product_permalink', 10, 3);

此过滤器挂钩可以更改任何特定产品/帖子类型或任何类别的永久链接结构。

© www.soinside.com 2019 - 2024. All rights reserved.