dongya9904 2018-10-18 19:37
浏览 59
已采纳

在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

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

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/

  • 写回答

1条回答 默认 最新

  • doujiu3882 2018-10-18 23:20
    关注

    Try combine:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法