WooCommerce - 将产品类别 slug 更改为商店基础
WooCommerce - change product-category slug to shop base
现在我已经尝试了两天来解决这个问题,并且快要失去理智了。
现在我的链接是 mydomain.com/product-category/clothing/t-shirts/
我希望它成为我的域。com/store/clothing/t-shirts/
我需要更改我的产品类别固定链接。但是当我在设置 - 永久链接 - 产品类别库中将其更改为 /store/ 时,我在尝试访问产品页面时收到 404。
非常感谢您的帮助!谢谢
在这里查看我的代码:http://levantoan.com/cach-cai-dat-base-cua-danh-muc-san-pham-giong-voi-base-cua-trang-san-pham/
或在githubhttps://gist.github.com/levantoan/fc705c5ae4739e6d87e2ec51b257ea5c
首先,转到设置/固定链接:
店铺基地:门店
产品类别基础:商店(与店铺基础相同)
产品永久链接库:带有类别的商店库,例如/商店/%product_cat%
然后在functions.php
中插入代码
//base product category same base shop Page for woocommerce
add_filter( 'rewrite_rules_array', function( $rules )
{
$new_rules = array();
$terms = get_terms( array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'hide_empty' => false,
));
if($terms && !is_wp_error($terms)){
$siteurl = esc_url(home_url('/'));
foreach ($terms as $term){
$term_slug = $term->slug;
$baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat'));
$new_rules[$baseterm.'?$'] = 'index.php?product_cat='.$term_slug;
$new_rules[$baseterm.'page/([0-9]{1,})/?$'] = 'index.php?product_cat='.$term_slug.'&paged=$matches[1]';
$new_rules[$baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?product_cat='.$term_slug.'&feed=$matches[1]';
}
}
return $new_rules + $rules;
} );
支持 Polylang 的解决方案。
根据@Toản Lê Văn 的说法。
add_filter( 'rewrite_rules_array', 'your_prefix_rewrite_rules', 9999 );
function your_prefix_rewrite_rules( $rules ) {
$new_rules = [];
$languages = pll_languages_list();
$your_base = 'shop';
foreach ( $languages as $lang ) {
$terms = get_terms(
[
'taxonomy' => 'product_cat',
'post_type' => 'product',
'hide_empty' => false,
'lang' => $lang,
]
);
if ( empty( $terms ) || is_wp_error( $terms ) ) {
continue;
}
$site_url = esc_url( home_url( '/' ) );
foreach ( $terms as $term ) {
$term_slug = $term->slug;
$base_term = str_replace( $site_url, '', get_term_link( $term->term_id, 'product_cat' ) );
$new_rules[ $base_term . '?$' ] = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug;
$new_rules[ $base_term . 'page/([0-9]{1,})/?$' ] = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug . '&paged=$matches[1]';
$new_rules[ $base_term . '(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug . '&feed=$matches[1]';
}
if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/?$' ] ) ) {
unset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/?$' ] );
}
if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/([^/]+)?(:/([0-9]+))?/?$' ] ) ) {
unset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/([^/]+)?(:/([0-9]+))?/?$' ] );
}
if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/.+?/[^/]+/([^/]+)/?$' ] ) ) {
unset( $rules[ '(' . $lang . ')/' . $your_base . '/.+?/[^/]+/([^/]+)/?$' ] );
}
}
return $new_rules + $rules;
}
现在我已经尝试了两天来解决这个问题,并且快要失去理智了。
现在我的链接是 mydomain.com/product-category/clothing/t-shirts/ 我希望它成为我的域。com/store/clothing/t-shirts/
我需要更改我的产品类别固定链接。但是当我在设置 - 永久链接 - 产品类别库中将其更改为 /store/ 时,我在尝试访问产品页面时收到 404。
非常感谢您的帮助!谢谢
在这里查看我的代码:http://levantoan.com/cach-cai-dat-base-cua-danh-muc-san-pham-giong-voi-base-cua-trang-san-pham/
或在githubhttps://gist.github.com/levantoan/fc705c5ae4739e6d87e2ec51b257ea5c
首先,转到设置/固定链接:
店铺基地:门店
产品类别基础:商店(与店铺基础相同)
产品永久链接库:带有类别的商店库,例如/商店/%product_cat%
然后在functions.php
中插入代码//base product category same base shop Page for woocommerce
add_filter( 'rewrite_rules_array', function( $rules )
{
$new_rules = array();
$terms = get_terms( array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'hide_empty' => false,
));
if($terms && !is_wp_error($terms)){
$siteurl = esc_url(home_url('/'));
foreach ($terms as $term){
$term_slug = $term->slug;
$baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat'));
$new_rules[$baseterm.'?$'] = 'index.php?product_cat='.$term_slug;
$new_rules[$baseterm.'page/([0-9]{1,})/?$'] = 'index.php?product_cat='.$term_slug.'&paged=$matches[1]';
$new_rules[$baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?product_cat='.$term_slug.'&feed=$matches[1]';
}
}
return $new_rules + $rules;
} );
支持 Polylang 的解决方案。
根据@Toản Lê Văn 的说法。
add_filter( 'rewrite_rules_array', 'your_prefix_rewrite_rules', 9999 );
function your_prefix_rewrite_rules( $rules ) {
$new_rules = [];
$languages = pll_languages_list();
$your_base = 'shop';
foreach ( $languages as $lang ) {
$terms = get_terms(
[
'taxonomy' => 'product_cat',
'post_type' => 'product',
'hide_empty' => false,
'lang' => $lang,
]
);
if ( empty( $terms ) || is_wp_error( $terms ) ) {
continue;
}
$site_url = esc_url( home_url( '/' ) );
foreach ( $terms as $term ) {
$term_slug = $term->slug;
$base_term = str_replace( $site_url, '', get_term_link( $term->term_id, 'product_cat' ) );
$new_rules[ $base_term . '?$' ] = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug;
$new_rules[ $base_term . 'page/([0-9]{1,})/?$' ] = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug . '&paged=$matches[1]';
$new_rules[ $base_term . '(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug . '&feed=$matches[1]';
}
if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/?$' ] ) ) {
unset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/?$' ] );
}
if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/([^/]+)?(:/([0-9]+))?/?$' ] ) ) {
unset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/([^/]+)?(:/([0-9]+))?/?$' ] );
}
if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/.+?/[^/]+/([^/]+)/?$' ] ) ) {
unset( $rules[ '(' . $lang . ')/' . $your_base . '/.+?/[^/]+/([^/]+)/?$' ] );
}
}
return $new_rules + $rules;
}