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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号