WordPress中单个自定义帖子类型的多个URL [英] Multiple URLs for Single Custom Post Type in WordPress

查看:58
本文介绍了WordPress中单个自定义帖子类型的多个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个房地产中介网站,其自定义帖子类型为 财产。因此,当前的URL结构为:

I have an estate agency property website that has a custom post type of 'property'. As such the current URL structure is:

/property/the-address-here

现在...在每个属性上,您可以选择该属性是出售还是出租。我现在想保留单个自定义帖子类型,但是对于单个帖子类型有两个URL:

Now... on each property you can choose whether the property is for sale or to rent. I now want to keep the single custom post type but have two URLs for the single post type:

/property-for-sale/the-address-here

/property-for-rent/the-address-here

我尝试将以下内容添加到我的.htaccess文件中:

I've tried adding the following to my .htaccess:

RewriteRule ^property-for-sale/([^/]*)/$ /index.php?name=$1 [QSA,L]
RewriteRule ^property-for-rent/([^/]*)/$ /index.php?name=$1 [QSA,L]

但是我只是被重定向回原始URL。

But I just get redirected back to the original URL.

此外,在有人推荐之前,我无法创建新的自定义帖子类型。

Also, I can't create a new custom post type before someone recommends that.

任何帮助表示赞赏!

推荐答案

在您的 functions.php 中尝试一下:

add_action('init', function()
{
    add_rewrite_rule('^property-([^/]+)/([^/]+)/?$', 'index.php?post_type=property&name=$matches[1]', 'top');
}, 0, 0);

请参见 add_rewrite_rule()了解更多信息。

这篇关于WordPress中单个自定义帖子类型的多个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆