Custom taxonomy and custom post type – wrong permalinks and template
so here is the deal – I’ve got a CPT called opencourses. It has some permalink structure, lets say: open-courses. Recently I’ve decided to add here a custom taxonomy as well. Im doing it via this code:
// Register Custom Taxonomy
function custom_taxonomy_blocks() {
$labels = array(
'name' => _x( 'Bloki szkoleniowe', 'Taxonomy General Name', 'lean' ),
'singular_name' => _x( 'Blok szkoleniowy', 'Taxonomy Singular Name', 'lean' ),
'menu_name' => __( 'Blok szkoleniowy', 'lean' ),
'all_items' => __( 'Wszystkie bloki', 'lean' ),
'parent_item' => __( 'Element nadrzędny', 'lean' ),
'parent_item_colon' => __( 'Element nadrzędny:', 'lean' ),
'new_item_name' => __( 'Nazwa nowego bloku', 'lean' ),
'add_new_item' => __( 'Dodaj nowy blok', 'lean' ),
'edit_item' => __( 'Edytuj blok', 'lean' ),
'update_item' => __( 'Aktualizuj blok', 'lean' ),
'view_item' => __( 'Zobacz blok', 'lean' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'lean' ),
'add_or_remove_items' => __( 'Add or remove items', 'lean' ),
'choose_from_most_used' => __( 'Choose from the most used', 'lean' ),
'popular_items' => __( 'Popular Items', 'lean' ),
'search_items' => __( 'Search Items', 'lean' ),
'not_found' => __( 'Not Found', 'lean' ),
'no_terms' => __( 'No items', 'lean' ),
'items_list' => __( 'Items list', 'lean' ),
'items_list_navigation' => __( 'Items list navigation', 'lean' ),
);
$rewrite = array(
'slug' => 'blok-szkoleniowy',
'with_front' => true,
'hierarchical' => true,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
);
register_taxonomy( 'blok-szkoleniowy', array( 'opencourses' ), $args );
}
add_action( 'init', 'custom_taxonomy_blocks', 0 );
And it works yet not as I assumed. These are my biggest pain points:
-
if cpt permalink is like: /open-courses, then i expect to work the taxonomy like within categories -> so the url will be like: /open-courses/taxonomy/nested-item-in-taxonomy etc. But it doesn’t. It simply generates permalink like /taxonomy. How to fix that?
-
Somehow the template for the custom post type doesnt work for the view of the taxonomy. How to define/fix that?
Leave an answer