我已经注册了一个自定义帖子类型“book”和一个分类法“authors”,如下所示:
add_action( \'init\', \'post_book_init\' );
function post_book_init() {
$labels = array(
\'name\' => \'BOOKs\',
\'singular_name\' => \'BOOK\',
\'menu_name\' => \'BOOKs\',
\'name_admin_bar\' => \'BOOK\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New BOOK\',
\'new_item\' => \'New BOOK\',
\'edit_item\' => \'Edit BOOK\',
\'view_item\' => \'View BOOK\',
\'all_items\' => \'All BOOKs\',
\'search_items\' => \'Search BOOKs\',
\'parent_item_colon\' => \'Parent BOOKs\',
\'not_found\' => \'No BOOK found.\',
\'not_found_in_trash\' => \'No BOOK found in Trash.\'
);
$args = array(
\'labels\' => $labels,
\'taxonomies\' => array(\'authors\'),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => false,
\'with_front\' => false,
\'capability_type\' => \'post\',
\'has_archive\' => \'bookarchives\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\',\'custom-fields\',\'comments\',\'revisions\',\'page-attributes\',\'post-formats\' )
);
register_post_type(\'book\', $args );
$labels = array(
\'name\' => \'Authors\',
\'singular_name\' => \'Author\',
\'search_items\' => \'Search Author\',
\'all_items\' => \'All Authors\',
\'parent_item\' => \'Parent Author\',
\'parent_item_colon\' => \'Parent Author:\',
\'edit_item\' => \'Edit Author\',
\'update_item\' => \'Update Author\',
\'add_new_item\' => \'Add New Author\',
\'new_item_name\' => \'New Author Name\',
\'menu_name\' => \'Author\'
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'authors\' ),
);
register_taxonomy(\'authors\', \'book\', $args );
global $wp_rewrite;
$book_structure = \'/book/%authors%/%book%-blah-blah-blah\';
$wp_rewrite->add_rewrite_tag("%book%", \'([^/]+)\', "book=");
$wp_rewrite->add_permastruct(\'book\', $book_structure, false);
unset($labels);
unset($args);
}
我想在permalink中使用分类术语,并在注册post&;后应用重写规则;分类学并使用以下代码替换了分类术语:
add_filter(\'post_type_link\', \'author_permalink_structure\', 10, 4);
function author_permalink_structure($post_link, $post, $leavename, $sample) {
if (false !== strpos($post_link, \'%authors%\')) {
$author_type_term = get_the_terms($post->ID, \'authors\');
if (!empty($author_type_term))
{
$author_slug = array_pop($author_type_term)->slug;
$parent_slug=get_term(get_term_by(\'slug\',$author_slug,\'authors\')->parent,\'authors\')->slug;
$post_link = str_replace(\'%authors%\', $author_slug, $post_link);
}
else
$post_link = str_replace(\'%authors%\', \'uncategorized\', $post_link);
}
return $post_link;
}
因此,以下链接可以很好地工作:localhost/book/[authors]/[book title]-blah blah blahandlocalhost/book/[authors],但我还需要具有子分类法的父分类法slug,如[父分类法slug]/[子分类法slug]我尝试将父分类法与;子元素使用“/”替换,但返回404页未找到,尽管使用“-”合并效果很好。
$author_slug = array_pop($author_type_term)->slug;
$parent_slug=get_term(get_term_by(\'slug\',$author_slug,\'authors\')->parent,\'authors\')->slug;
$post_link = str_replace(\'%authors%\', $parent_slug . "-" .$author_slug , $post_link);
How can I use "[parent-taxonomy-slug]/[child-taxonomy-slug]" in permalink?