长话短说,这个问题的答案是肯定的。
最后,我能够创建:tax-1/tax-2/cpt-1/cpt-1-post/cpt-2/cpt-2-post/
还有另一个结构:tax-1/tax-2/cpt-1/cpt-1-post/cpt-2/custom field/cpt-2-post/
我使用的类型来自http://wp-types.com/ 要创建分层自定义分类法,需要自定义帖子类型、自定义字段,但最重要的是自定义帖子类型之间的父/子关系。那个插件节省了很多时间。
下一步是使用正则表达式重新写入URL。
<?php
// Add custom rewrite rules
add_filter( \'rewrite_rules_array\',\'my_insert_rewrite_rules\' );
// Add custom variable for WP_QUERY
add_filter( \'query_vars\',\'my_insert_query_vars\' );
// This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated.
add_action( \'wp_loaded\',\'my_flush_rules\' );
// Flush rules for include custom rewrite rules
function my_flush_rules(){
$rules = get_option( \'rewrite_rules\' );
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// Add custom rewrite rules
function my_insert_rewrite_rules( $rules ){
$newrules = array();
$location_list = get_terms("location",array("parent" => 0, "hide_empty" => 0));
foreach($location_list as $location){
$base_url = sprintf(\'index.php?taxonomy=%s&term=%s&child_term=$matches[1]&type=$matches[2]&type_title=$matches[3]&media_type=$matches[4]&media_maintitle=$matches[5]&media_title=$matches[6]\',\'location\',$location->slug);
$newrules[$location->slug.\'/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?\'] = $base_url;
$base_url = sprintf(\'index.php?taxonomy=%s&term=%s&child_term=$matches[1]&type=$matches[2]&type_title=$matches[3]&media_type=$matches[4]&media_title=$matches[5]\',\'location\',$location->slug);
$newrules[$location->slug.\'/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?\'] = $base_url;
$base_url = sprintf(\'index.php?taxonomy=%s&term=%s&child_term=$matches[1]&type=$matches[2]&type_title=$matches[3]&media_type=$matches[4]\',\'location\',$location->slug);
$newrules[$location->slug.\'/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?\'] = $base_url;
$base_url = sprintf(\'index.php?taxonomy=%s&term=%s&child_term=$matches[1]&type=$matches[2]&type_title=$matches[3]\',\'location\',$location->slug);
$newrules[$location->slug.\'/([^/]+)/([^/]+)/([^/]+)/?\'] = $base_url;
$base_url = sprintf(\'index.php?taxonomy=%s&term=%s&child_term=$matches[1]&type=$matches[2]\',\'location\',$location->slug);
$newrules[$location->slug.\'/([^/]+)/([^/]+)/?\'] = $base_url;
$base_url = sprintf(\'index.php?taxonomy=%s&term=%s&child_term=$matches[1]\',\'location\',$location->slug);
$newrules[$location->slug.\'/([^/]+)/?\'] = $base_url;
}
return $newrules + $rules;
}
// Add custom variable for WP_QUERY
function my_insert_query_vars( $vars ){
array_push($vars, \'taxonomy\');
array_push($vars, \'child_term\');
array_push($vars, \'type\');
array_push($vars, \'type_title\');
array_push($vars, \'media_type\');
array_push($vars, \'media_maintitle\');
array_push($vars, \'media_title\');
return $vars;
}
?>
我不确定这里是否创建了最佳的REGEX规则,但我发现我必须做很多事情来确保重新定向到位,确保没有多个URL显示相同内容的可能性。
答案比我在这里发布的更多,但我认为我可以发布的行数是有限的。