在wordpress中重写URL 2次
问题描述:
I have a functionality on first page it lists all states on click of any states in next page it lists cities related to that state and on click f city it shows city page.
State list page - https://benefitsexplorer.com/get-help/unclaimed-money/
originally url for city listing page was
https://benefitsexplorer.com/cities/?lp=unclaimed-money&state=North%20Carolina/
I have added url rewrites successfully for this page using this code
1 2 3 4 5 6 7 8 9 10 11 12 |
function rewrite_state_url(){ add_rewrite_rule(\'^get-help/([^/]*)/([^/]*)/?\',\'index.php? page_id=2931&lp=$matches[1]&state=$matches[2]\',\'top\'); } function register_custom_query_vars($vars){ array_push($vars, \'lp\'); array_push($vars, \'state\'); return $vars; } add_action(\'init\',\'rewrite_state_url\',1); add_filter(\'query_vars\', \'register_custom_query_vars\',1); |
So its showing url like i wanted
https://benefitsexplorer.com/get-help/unclaimed-money/North%20Carolina
But i am trying now same for city page which will take parameter lastpage, state and city.
so i have added code like
1 2 3 4 5 6 7 8 9 10 11 12 |
function rewrite_city_url(){ add_rewrite_rule(\'^get-help/([^/]*)/([^/]*)/([^/]*)/?\',\'index.php?page_id=2944&lp=$matches[1]&state=$matches[2]&city=$matches[3]\',\'top\'); } function register_custom_query_vars1($vars){ array_push($vars, \'lp\'); array_push($vars, \'state\'); array_push($vars, \'city\'); return $vars; } add_action(\'init\',\'rewrite_city_url\',2); add_filter(\'query_vars\', \'register_custom_query_vars1\',2); |
now its showing url https://benefitsexplorer.com/get-help/unclaimed-money/North%20Carolina/Locust/
but its showing same page which is showing at state list url https://benefitsexplorer.com/cities/?lp=unclaimed-money$state=North%20Carolina/
网友观点:
Try combine:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function rewrite_state_url(){ add_rewrite_rule(\'^get-help/([^/]*)/([^/]*)/([^/]*)/?\',\'index.php?page_id=2944&lp=$matches[1]&state=$matches[2]&city=$matches[3]\',\'top\'); add_rewrite_rule(\'^get-help/([^/]*)/([^/]*)/?\',\'index.php?page_id=2931&lp=$matches[1]&state=$matches[2]\',\'top\'); } function register_custom_query_vars($vars){ array_push($vars, \'lp\'); array_push($vars, \'state\'); array_push($vars, \'city\'); return $vars; } add_action(\'init\',\'rewrite_state_url\',1); add_filter(\'query_vars\', \'register_custom_query_vars\',1); |
Also flush your permalinks (admin > settings > permalinks and hit save without changing anything)
The order in the add_rewrite_rule is important, so do not change it! And perhaps change your function name because now it contains state AND city :)
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » 在wordpress中重写URL 2次