重写查询字符串以允许wordpress

时间:2018-04-26 16:47:15

标签: php regex wordpress url-rewriting

我正在看这篇文章

Multiple URLs for Single Custom Post Type in WordPress

我有一个非常相似的情况,但我不能让这个结果起作用......我有一个带有分类的自定义帖子类型,我用它作为查询字符串来控制页面上的内容。

我想要同一篇文章的不同网址。

因此...

luxury-yachts/my-yacht-name/?yacht-type=luxury-yachts-for-sale
luxury-yachts/my-yacht-name/?yacht-type=luxury-yachts-charter

理想情况下,想要重写

luxury-yachts/luxury-yachts-for-sale/my-yacht-name/
luxury-yachts/luxury-yachts-charter/my-yacht-name/

我承认我不是正则表达式的专家,但我尝试的所有东西似乎都做不了......

非常感谢任何帮助

编辑 -

我试过这个:

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

}, 0, 0);

但没有快乐

2 个答案:

答案 0 :(得分:0)

试试正则表达式:

^luxury-yachts(?:\/(?:[^\/]+))+?\/?$

Demo

答案 1 :(得分:0)

给出你的正则表达式输入字符串:

luxury-yachts/my-yacht-name/?yacht-type=luxury-yachts-for-saleluxury-yachts/my-yacht-name/?yacht-type=luxury-yachts-charter

模式:~^([^/]+)/([^/]+)/\?yacht-type=(.+?)/?$~ 替换:$1/$3/$2/

Pattern Demo

基于此参考:https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

我想你想要这个:

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

或者:

add_action('init', function() {
    add_rewrite_rule('^([^/]+)/([^/]+)/\?yacht-type=(.+?)/?$', '$matches[1]/$matches[3]/$matches[2]/', 'top');
}, 0, 0);