I have created a custom taxonomy URL structure that is as follows:
/term_slug/post_name
I have created custom rewrite rules to do this, as follows:
register_taxonomy( 'blog_type', 'post', array(
'labels' => array(
'name' => _x( 'Blog Types', 'taxonomy general name' ),
'singular_name' => _x( 'Blog Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Blog Types' ),
'all_items' => __( 'All Blog Types' ),
'parent_item' => __( 'Parent Blog Type' ),
'parent_item_colon' => __( 'Parent Blog Type:' ),
'edit_item' => __( 'Edit Blog Type' ),
'update_item' => __( 'Update Blog Type' ),
'add_new_item' => __( 'Add New Blog Type' ),
'new_item_name' => __( 'New Blog Type Name' ),
'menu_name' => __( 'Blog Type' ),
),
'rewrite' => false,
'hierarchical' => true,
) );
add_rewrite_tag( "%blog_type%", '(.+?)', "blog_type=" );
// Get all the blog types
$blog_types = get_terms( 'blog_type', array( 'hide_empty' => false, ) );
if( ! empty( $blog_types ) ) {
foreach( $blog_types as $blog_type ) {
add_rewrite_rule( "$blog_type->slug/?$", "index.php?post_type=post&taxonomy=blog_type&term=$blog_type->slug", 'top' );
add_rewrite_rule( "$blog_type->slug/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=post&taxonomy=blog_type&term=$blog_type->slug&paged=$matches[1]", 'top' );
add_permastruct( "%$blog_type->slug%", "$blog_type->slug/%postname%", false );
}
}
This works fine and dandy, except I need to include sub-terms in the URL structure as well, so for example:
/term_slug/term_subterm_slug/post_name
This would include multiple-dimensional taxonomies in which terms have sub-terms. I am a bit stuck at the moment, any help?
I have looked at this post but it does not elaborate on how to create the URL structure with a dynamic amount of sub-terms.