繁体   English   中英

URL在wordpress中重写2次

[英]URL rewrite in wordpress 2 times

我在第一页上有一个功能,它在单击任何状态时都会列出所有州,在下一页中它会列出与该州相关的城市,并在单击f城市时会显示城市页面。 状态列表页面-https://benefitsexplorer.com/get-help/unclaimed-money/

城市列表页面的原始网址是https://benefitsexplorer.com/cities/?lp=unclaimed-money&state=North%20Carolina/

我已使用此代码成功为该页面添加了URL重写

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);

所以它显示的网址就像我想要的https://benefitsexplorer.com/get-help/unclaimed-money/North%20Carolina

但是我现在正在为城市页面尝试同样的方法,它将采用参数lastpage,州和城市。

所以我添加了类似的代码

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);

现在其显示网址https://benefitsexplorer.com/get-help/unclaimed-money/North%20Carolina/Locust/

但其显示的同一页面显示在状态列表URL中https://benefitsexplorer.com/cities/?lp=unclaimed-money $ state = North%20Carolina /

尝试结合:

 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);

同时刷新您的永久链接(管理>设置>永久链接,然后点击保存而不更改任何内容)

add_rewrite_rule中的顺序很重要,因此请勿更改! 也许更改函数名称,因为现在它包含州和城市:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM