在slug的“有意义”url变量(城市、地区等)之前添加所需的值,如下所示:
"uk/([^/]*)/([^/]*)/?$" => \'index.php?city=$matches[1]&area=$matches[2]&something=$matches[3]\',
这样,如果“uk”在开头,它将只“捕获”那些URL变量(城市、地区)。
更完整的示例可能如下所示
//Set up the additional values in the "rewrite rules" array
function my_rewrites($rules){
$new_rules = array(
//uk/London/Chelsea/
"uk/([^/]*)/([^/]*)/?$" => \'index.php?city=$matches[1]&area=$matches[2]\',
);
$new_rules = array_merge($new_rules, $rules);
return $new_rules;
}
add_filter(\'rewrite_rules_array\', \'my_rewrites\');
/**
* Add Rewrite Tags
*/
function my_rewrite_tags(){
//EG "London"
add_rewrite_tag(\'%city%\',\'([^/]*)\');
//EG "Chelsea"
add_rewrite_tag(\'%area%\',\'([^/]*)\');
}
add_action(\'init\', \'my_rewrite_tags\');
/**
* Use the information provided in the URL like this:
*/
function my_custom_function_which_does_stuff(){
global $wp_query;
if ( isset( $wp_query->query_vars[\'city\'] ) ){
//EG London
$city = $wp_query->query_vars[\'city\'];
//EG Chelsea
$area = $wp_query->query_vars[\'area\'];
}
}
add_action( \'init\', \'my_custom_function_which_does_stuff\' );