不存在类别URL与“商店基础”与类别合作。

发布于 2025-01-28 20:05:41 字数 449 浏览 3 评论 0原文

我在永久链接设置中设置了“带有类别的商店基础”选项。

现在我的产品URL就是这样:

site.com/shop/category1/product1

但是,此URL也转到Product1:

site.com/shop/randomword/product1

没有类别这样的“随机字”,也没有site.com/shop/randomword/给出404个错误,但上述URL工作。它不会重定向到 /category1 /product1,而只是有效。

因此,remove_filter('template_redirect','redirect_caronical');不起作用。它适用于其他帖子,但不适合产品。

有什么方法可以防止这种情况吗?

I set "Shop base with category" option in the Permalink Settings.

Now my product URLs are like that:

site.com/shop/category1/product1

However, this URL also goes to product1:

site.com/shop/randomword/product1

There is no category such "randomword" and also site.com/shop/randomword/ gives 404 error but the URL above works. It does not redirect to /category1/product1, it just works.

Therefore remove_filter( 'template_redirect', 'redirect_canonical' ); is not working. It works for other posts but not products.

Is there any way to prevent this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2025-02-04 20:05:41

我找到了解决方案。

该页面可与随机类别URL一起使用。因此,当URL不等于永久链接时,我决定将其重定向到永久链接。因此,当有一个随机URL时,它将重定向到实际URL。

<?php 
     global $wp;
     $current_url = home_url( $wp->request );
     $actual_url = get_permalink();
     if ((is_product()) && ($current_url !== $actual_url)) {
         wp_redirect( $actual_url, 301 );
         exit();
} ?>

您可以将此代码放在header.php中

输入时,

site.com/shop/randomword/product1

它重定向到

site.com/shop/category1/product1

编辑:

代码打破了草案预览。需要指定它已发布。

global $wp;
$current_url = home_url( $wp->request );
$actual_url = get_permalink();
$current_post_status = get_post_status();
if ((is_product()) && ($current_post_status == 'publish' ) && ($current_url !== $actual_url)) {
   wp_redirect( $actual_url, 301 );
   exit();
}

I found a solution for this.

The page works with a random category URL. So when the URL is not equal to permalink I decide to redirect to permalink. So when there is a random URL, it redirects to actual URL.

<?php 
     global $wp;
     $current_url = home_url( $wp->request );
     $actual_url = get_permalink();
     if ((is_product()) && ($current_url !== $actual_url)) {
         wp_redirect( $actual_url, 301 );
         exit();
} ?>

You can put this code in the header.php

When you enter

site.com/shop/randomword/product1

it redirects to

site.com/shop/category1/product1

Edit:

The code breaks the draft preview. Need to specify it is published.

global $wp;
$current_url = home_url( $wp->request );
$actual_url = get_permalink();
$current_post_status = get_post_status();
if ((is_product()) && ($current_post_status == 'publish' ) && ($current_url !== $actual_url)) {
   wp_redirect( $actual_url, 301 );
   exit();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文