I'm adding rewrite rules to my PHP script which is included in a WordPress page with the permalink kb
So I can visit domain.com/kb and the page is displayed.
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb/([^/]+)/?$', 'kb?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
But when I visit the page with additional strings in the url, I get a 404.
So when I visit domain.com/kb is shows the correct page, and then visiting domain.com/kb/84/92, it shows a 404
I just need to be able to read the additional url params in my PHP script, such as $_GET["kb_cat"]
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb$', 'index.php?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
to take it a step further and use the parameters:
function add_query_vars_filter( $vars ){
$vars[] = "kb_cat";
$vars[] = "kb_seq";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
and then load a custom template file:
function include_custom_template($template){
if(get_query_var('kb_cat') && get_query_var('kb_seq')){
$template = get_template_directory() ."/my-custom-template.php";
}
return $template;
}
add_filter('template_include', 'include_custom_template');
Once adding to your functions.php
go to Settings > Permalinks and hit 'save changes' to reset the flush rules
function include_custom_template($template){ if(get_query_var('kb_cat') && get_query_var('kb_seq')){ $template = get_template_directory() ."/my-custom-template.php"; } return $template; } add_filter('template_include', 'include_custom_template');
try this:
function wdm_add_rewrite_rules() {
add_rewrite_rule( '^kb\/([^\/]+)\/?([^\/]+)$', 'kb?kb_cat=$matches[1]&kb_seq=1', 'top');
}
add_action('init', 'wdm_add_rewrite_rules');
You can check the regular expression on https://regex101.com/ or any other websites like this online for the matches
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.