您可以尝试以下操作:
function my_car_rewrite_rules( $rules ) {
$newrules = array();
// add a rule for this kind of url:
// http://myhost.com/cars/ferrari/used/123
// => http://myhost.com/index.php?post_type=cars&ferrari=used&p=123
$newrules[\'^cars/([^/]+)/([^/]+)/([^/]+)$\'] = \'index.php?post_type=cars&$matches[1]=$matches[2]&p=$matches[3]\';
return $newrules + $rules;
}
add_filter(\'rewrite_rules_array\',\'my_car_rewrite_rules\');
测试时请记住“保存永久链接”。
我补充道/cars/ 因此,此重写不会覆盖其他页面。
您还可以尝试此更严格的规则:
$newrules[\'^cars/([a-z]+)/([a-z]+)/([0-9]+)$\'] = \'index.php?post_type=cars&$matches[1]=$matches[2]&p=$matches[3]\';
甚至
$newrules[\'^cars/([ferrari|lamborghini]+)/([used|new]+)/([0-9]+)$\'] = \'index.php?post_type=cars&$matches[1]=$matches[2]&p=$matches[3]\';
要从URI中删除CPT(需要为每个分类法执行此操作)
$newrules[\'^ferrari/([^/]+)/([^/]+)$\'] = \'index.php?post_type=cars&$ferrari=$matches[1]&p=$matches[2]\';