没有我想的那么难。下面是一个工作示例,以防任何人发现它有帮助:
function em_query_vars($vars) {
array_push($vars, \'state\');
array_push($vars, \'city\');
return $vars;
}
add_filter(\'query_vars\',\'em_query_vars\');
function em_rewrite() {
add_rewrite_rule( \'^state/([^/]*)/([^/]*)/?\', \'index.php?state=$matches[1]&city=$matches[2]\', \'top\' );
add_rewrite_rule( \'^state/([^/]*)/?\', \'index.php?state=$matches[1]\', \'top\' );
add_rewrite_tag(\'%state%\',\'([^&]+)\');
add_rewrite_tag(\'%city%\',\'([^&]+)\');
}
add_action(\'init\', \'em_rewrite\');
function em_templates($template) {
global $wp_query;
if (isset($wp_query->query_vars[\'state\']) && isset($wp_query->query_vars[\'city\'])) {
return dirname(__FILE__) . \'/single-city.php\';
} elseif (isset($wp_query->query_vars[\'state\'])) {
return dirname(__FILE__) . \'/single-state.php\';
}
return $template;
}
add_filter(\'template_include\', \'em_templates\', 1, 1);