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()了解更多信息。