admin管理员组文章数量:1279145
Using add_rewrite_rule() to handle things like this for the custom post type "catalogs", page name "cars":
/catalogs/cars/ /catalogs/cars/p1/ /catalogs/cars/p1/p2/ /catalogs/cars/p1/p2/p3/
I never have more than 3 parameters after /cars/, but all of them with optional query string parameters. Like this:
/catalogs/cars/p1/p2/?weight-max=3200
Here's my code:
add_rewrite_rule(
'^catalogs/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?',
'index.php?catalogs=$matches[1]&p1=$matches[2]&p2=$matches[3]&p3=$matches[4]',
'top'
);
Works great for when there's 3 parameters (like /catalogs/cars/p1/p2/p3/), but it doesn't work for when there's 0-2 parameters.
Have tried making parts of the regex optional, but I guess my regex skills aren't that fantastic...
Have a trick for making the number of parameters dynamic, so everything from 0-3 parameters can be handled?
Thanks :thumbsup:
Using add_rewrite_rule() to handle things like this for the custom post type "catalogs", page name "cars":
/catalogs/cars/ /catalogs/cars/p1/ /catalogs/cars/p1/p2/ /catalogs/cars/p1/p2/p3/
I never have more than 3 parameters after /cars/, but all of them with optional query string parameters. Like this:
/catalogs/cars/p1/p2/?weight-max=3200
Here's my code:
add_rewrite_rule(
'^catalogs/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?',
'index.php?catalogs=$matches[1]&p1=$matches[2]&p2=$matches[3]&p3=$matches[4]',
'top'
);
Works great for when there's 3 parameters (like /catalogs/cars/p1/p2/p3/), but it doesn't work for when there's 0-2 parameters.
Have tried making parts of the regex optional, but I guess my regex skills aren't that fantastic...
Have a trick for making the number of parameters dynamic, so everything from 0-3 parameters can be handled?
Thanks :thumbsup:
Share Improve this question asked Sep 25, 2021 at 13:35 MadsMads 11 bronze badge1 Answer
Reset to default 0Found the solution, just had to remember all the ? marks.
It works when the regex is like this:
'index.php?catalogs=$matches[1]&p1=$matches[2]&p2=$matches[3]&p3=$matches[4]'
本文标签: url rewritingaddrewriterule with optional parameters
版权声明:本文标题:url rewriting - add_rewrite_rule with optional parameters 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741301258a2371102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论