经过多次努力,我已经解决了这个问题,但这并不完全是我想要的,因为我不得不在一件事上妥协,并在URL中传递了一些分隔符。示例:
example.com/separator1/parentatx/childtax/separator2/postname/
为了避免404错误,在分页和其他自定义分类法的情况下,我创建了一些重写规则。这是我的代码:
第一个帖子类型和自定义分类注册码:
register_post_type(
\'article\',
array(
\'public\' => true,
\'hierarchical\' => true,
\'menu_position\' => 15,
\'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
\'taxonomies\' => array( \'\' ),
\'menu_icon\' => get_template_directory_uri().\'/img/icon_article.png\',
\'query_var\' => true
\'rewrite\' => array(
\'slug\' => \'articles/%article_category%/topic/\',
\'with_front\' => true
),
\'has_archive\' => \'/all-articles/\',
\'register_meta_box_cb\' => \'add_article_metaboxes\'
)
);
register_taxonomy(
\'article_category\',
\'article\',
array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'/articles/\', \'hierarchical\' => true ),
)
);
在第一个自定义分类法中,我使用“articles”和“topic”作为分隔符;您可以根据您的场景使用任意选项。
第二个帖子类型和自定义分类注册码:
register_post_type(
\'gallery\',
array(
\'public\' => true,
\'menu_position\' => 15,
\'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\' ),
\'taxonomies\' => array( \'\' ),
\'menu_icon\' => get_template_directory_uri().\'/img/icon_gallery.png\',
\'query_var\' => true,
\'rewrite\' => array(
\'slug\' => \'galleries/%gallery_category%/images/\',
\'with_front\' => true
),
\'has_archive\' => \'/all-galleries/\',
\'register_meta_box_cb\' => \'add_gallery_metaboxes\'
)
);
在第二个自定义分类法中,我使用“图库”和“图像”作为分隔符;您可以根据您的场景使用任意选项。
重写规则的功能:
add_filter( \'rewrite_rules_array\', \'mmp_rewrite_rules\' );
function mmp_rewrite_rules( $rules ) {
$newRules = array();
//Rules for First Taxonomy
$newRules[\'articles/(.+)/(.+)/(topic)/(.+)/?$\'] = \'index.php?article=$matches[4]\';
$newRules[\'articles/(.+)/(topic)/(.+)/?$\'] = \'index.php?article_category=$matches[1]&article=$matches[3]\';
$newRules[\'articles/(.+)/(.+)/?([0-9]{1,})/?$\'] = \'index.php?article_category=$matches[1]&page=$matches[3]\';
$newRules[\'articles/(.+)/(.+)/(page)/?([0-9]{1,})/?$\'] = \'index.php?article_category=$matches[1]&page=$matches[4]\';
$newRules[\'articles/(.+)/?$\'] = \'index.php?article_category=$matches[1]\';
//Rules for Second Taxonomy
$newRules[\'galleries/(.+)/(.+)/(images)/(.+)/?$\'] = \'index.php?gallery=$matches[4]\';
$newRules[\'galleries/(.+)/(images)/(.+)/?$\'] = \'index.php?gallery_category=$matches[1]&gallery=$matches[3]\';
$newRules[\'galleries/(.+)/(.+)/?([0-9]{1,})/?$\'] = \'index.php?gallery_category=$matches[1]&page=$matches[3]\';
$newRules[\'galleries/(.+)/(.+)/(page)/?([0-9]{1,})/?$\'] = \'index.php?gallery_category=$matches[1]&page=$matches[4]\';
$newRules[\'galleries/(.+)/?$\'] = \'index.php?gallery_category=$matches[1]\';
return array_merge( $newRules, $rules );
}
根据自定义分类层次结构更改自定义帖子类型URL的函数:
function filter_post_type_link( $link, $post ) {
if ( $post->post_type != \'article\' && $post->post_type != \'gallery\' )
return $link;
if ( $post->post_type == \'article\' ) {
if ( $cats = get_the_terms( $post->ID, \'article_category\' ) ) {
$link = str_replace( \'%article_category%\', get_taxonomy_parents( array_pop( $cats )->term_id,
\'article_category\', false, \'/\', true ), $link ); // See custom function defined below
}
return $link;
} elseif ( $post->post_type == \'gallery\' ) {
if ( $cats = get_the_terms( $post->ID, \'gallery_category\' ) ) {
$link = str_replace(\'%gallery_category%\', get_taxonomy_parents( array_pop( $cats )->term_id,
\'gallery_category\', false, \'/\', true ), $link); // See custom function defined below
}
return $link;
}
}
add_filter( \'post_type_link\', \'filter_post_type_link\', 10, 2 );
// My own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents( $id, $taxonomy, $link = false, $separator = \'/\', $nicename = false, $visited = array() ) {
$chain = \'\';
$parent = &get_term( $id, $taxonomy );
if ( is_wp_error( $parent ) ) {
return $parent;
}
if ( $nicename )
$name = $parent -> slug;
else
$name = $parent -> name;
if ( $parent -> parent && ( $parent -> parent != $parent -> term_id ) && !in_array( $parent -> parent, $visited ) ) {
$visited[] = $parent -> parent;
$chain .= get_taxonomy_parents( $parent -> parent, $taxonomy, $link, $separator, $nicename, $visited );
}
if ( $link ) {
// Nothing, can\'t get this working :(
} else {
$chain .= $name . $separator;
}
return $chain;
}