简体   繁体   English

WordPress的add_rewrite_rule不导航到正确的页面

[英]wordpress add_rewrite_rule not navigating to correct page

I need to change this url in: " http://example.com/class-schedules?selectedCatId=52 " to become: " http://example.com/class-schedules/classCSCP " , but it navigates to incorrect page. 我需要在以下网址中更改此网址:“ http://example.com/class-schedules?selectedCatId=52 ”才能变为:“ http://example.com/class-schedules/classCSCP ”,但它会导航到错误的页面。 Any help !! 任何帮助!

I used this method in order to rewrite the url 我使用此方法来重写网址

function events_custom_rewrite() {
add_rewrite_rule("class-schedules/classCPA/?", 'index.php?page_id=4321&selectedCatId=52', 'top');
}

You need the add_rewrite_tag too. 您也需要add_rewrite_tag

Please include this code and clear the permalink cache in your settings. 请包括此代码,并清除设置中的永久链接缓存。

function custom_rewrite_tag() {
  add_rewrite_tag('%selectedCatId%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

function custom_rewrite_rule() {
    add_rewrite_rule('^class-schedules/classCPA/([^/]*)/([^/]*)/?','index.php?page_id=4321&selectedCatId=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

With this code you should ( I didn't test it! ) access your site like this: 使用此代码,您应该( 我没有对其进行测试! )像这样访问您的网站:

http://example.com/class-schedules/classCPA/52 http://example.com/class-schedules/classCPA/52

And in your theme you can access the variable like this: 在您的主题中,您可以像这样访问变量:

global $wp_query;
$selectedCatId = $wp_query->query_vars['selectedCatId']; //52

IMPORTANT: Do not forget to flush and regenerate the rewrite rules database after modifying rules. 重要说明:不要忘记在修改规则后刷新并重新生成重写规则数据库。 From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes. 在WordPress管理屏幕上,选择设置->永久链接,然后单击保存更改而不进行任何更改。

https://codex.wordpress.org/Rewrite_API/add_rewrite_tag https://codex.wordpress.org/Rewrite_API/add_rewrite_rule https://codex.wordpress.org/Rewrite_API/add_rewrite_tag https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

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

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