How to create nested taxonomy for CPT without using plugin

[ad_1]

Setting up a site which will have a product catalogue but will never need a cart and checkout. So to avoid using WooCommerce to get this functionality out of the box I thought nested taxonomies and a CPT would do the trick. WooCommerce is big and if I can avoid using it that is nice. I know I can use a plugin like CPTUI to add this but I would prefer to just know how to achieve this manually to keep the install lean.

Most guides I can find just say “Use a plugin”. I tried myself using generateWP for the CPT and taxonomy generation and I put it in a simple plugin. But the result is still “domain.com/product/product” Where the category is just the slug for the “Products”-post type.

The desired result is domain.com/productcategory/product
or domain.com/productcategory/productcategory/product when nested.

I feel like there is some rewrite function that is missing based on what few discussions I could find on this online.

Attached the relevant content from the plugin file below:

// Custom Taxonomy Produktkategori
if ( ! function_exists( ‘produkt_kategori’ ) ) {

// Register Custom Taxonomy
function produkt_kategori() {

$labels = array(
‘name’ => _x( ‘Produktkategorier’, ‘Taxonomy General Name’, ‘flow-produktkategori’ ),
‘singular_name’ => _x( ‘Produktkategori’, ‘Taxonomy Singular Name’, ‘flow-produktkategori’ ),
‘menu_name’ => __( ‘Produktkategorier’, ‘flow-produktkategori’ ),
‘all_items’ => __( ‘Alle produktkategorier’, ‘flow-produktkategori’ ),
‘parent_item’ => __( ‘Foreldre produkt’, ‘flow-produktkategori’ ),
‘parent_item_colon’ => __( ‘Foreldreprodukt:’, ‘flow-produktkategori’ ),
‘new_item_name’ => __( ‘Ny produktkategori’, ‘flow-produktkategori’ ),
‘add_new_item’ => __( ‘Legg til ny produktkategori’, ‘flow-produktkategori’ ),
‘edit_item’ => __( ‘Rediger produktkategori’, ‘flow-produktkategori’ ),
‘update_item’ => __( ‘Oppdater produktkategori’, ‘flow-produktkategori’ ),
‘view_item’ => __( ‘Se produktkategori’, ‘flow-produktkategori’ ),
‘separate_items_with_commas’ => __( ‘Separer kategorier med komma’, ‘flow-produktkategori’ ),
‘add_or_remove_items’ => __( ‘Legg til eller fjern kategorier’, ‘flow-produktkategori’ ),
‘choose_from_most_used’ => __( ‘Velg fra mest brukte’, ‘flow-produktkategori’ ),
‘popular_items’ => __( ‘Populære produktkategorier’, ‘flow-produktkategori’ ),
‘search_items’ => __( ‘Søk i produktkategorier’, ‘flow-produktkategori’ ),
‘not_found’ => __( ‘Fant ingen resultat’, ‘flow-produktkategori’ ),
‘no_terms’ => __( ‘Ingen produktkategorier’, ‘flow-produktkategori’ ),
‘items_list’ => __( ‘produktkategori liste’, ‘flow-produktkategori’ ),
‘items_list_navigation’ => __( ‘navigasjon i produktkategoriliste’, ‘flow-produktkategori’ ),
);
$rewrite = array(
‘slug’ => ‘produktkategori’,
‘with_front’ => false,
‘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( ‘produktkategori’, array( ‘flow_produkter’ ), $args );

}
add_action( ‘init’, ‘produkt_kategori’, 0 );

}

// Product Custom Post Type
if ( ! function_exists(‘flow_produkter’) ) {

// Register Custom Post Type
function flow_produkter() {

$labels = array(
‘name’ => _x( ‘Produkter’, ‘Post Type General Name’, ‘flow-produkter’ ),
‘singular_name’ => _x( ‘Produkt’, ‘Post Type Singular Name’, ‘flow-produkter’ ),
‘menu_name’ => __( ‘Produkter’, ‘flow-produkter’ ),
‘name_admin_bar’ => __( ‘Produkt’, ‘flow-produkter’ ),
‘archives’ => __( ‘Produktarkiv’, ‘flow-produkter’ ),
‘attributes’ => __( ‘Produktattributter’, ‘flow-produkter’ ),
‘parent_item_colon’ => __( ‘Foreldreprodukt’, ‘flow-produkter’ ),
‘all_items’ => __( ‘Alle produkter’, ‘flow-produkter’ ),
‘add_new_item’ => __( ‘Legg til nytt produkt’, ‘flow-produkter’ ),
‘add_new’ => __( ‘Legg til ny’, ‘flow-produkter’ ),
‘new_item’ => __( ‘Nytt produkt’, ‘flow-produkter’ ),
‘edit_item’ => __( ‘Rediger produkt’, ‘flow-produkter’ ),
‘update_item’ => __( ‘Oppdater produkt’, ‘flow-produkter’ ),
‘view_item’ => __( ‘Se produkt’, ‘flow-produkter’ ),
‘view_items’ => __( ‘Se produkter’, ‘flow-produkter’ ),
‘search_items’ => __( ‘Søk etter produkt’, ‘flow-produkter’ ),
‘not_found’ => __( ‘Fant ingen resultat’, ‘flow-produkter’ ),
‘not_found_in_trash’ => __( ‘Fant ingen resultat’, ‘flow-produkter’ ),
‘featured_image’ => __( ‘Fremheved bilde’, ‘flow-produkter’ ),
‘set_featured_image’ => __( ‘Velg fremhevet bilde’, ‘flow-produkter’ ),
‘remove_featured_image’ => __( ‘Fjern fremhevet bilde’, ‘flow-produkter’ ),
‘use_featured_image’ => __( ‘Bruk som fremhevet bilde’, ‘flow-produkter’ ),
‘insert_into_item’ => __( ‘Sett inn i produkt’, ‘flow-produkter’ ),
‘uploaded_to_this_item’ => __( ‘Lastet opp til produkt’, ‘flow-produkter’ ),
‘items_list’ => __( ‘Produktliste’, ‘flow-produkter’ ),
‘items_list_navigation’ => __( ‘Produktlistenavigasjon’, ‘flow-produkter’ ),
‘filter_items_list’ => __( ‘Filtrer produktliste’, ‘flow-produkter’ ),
);
$rewrite = array(
‘slug’ => ‘produkt’,
‘with_front’ => true,
‘pages’ => true,
‘feeds’ => true,
);
$args = array(
‘label’ => __( ‘Produkt’, ‘flow-produkter’ ),
‘description’ => __( ‘Informasjon om produkter’, ‘flow-produkter’ ),
‘labels’ => $labels,
‘supports’ => array( ‘title’, ‘thumbnail’, ‘revisions’, ‘custom-fields’, ‘page-attributes’ ),
‘taxonomies’ => array( ‘produktkategori’, ‘post_tag’ ),
‘hierarchical’ => true,
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘menu_position’ => 25,
‘menu_icon’ => ‘dashicons-cart’,
‘show_in_admin_bar’ => true,
‘show_in_nav_menus’ => true,
‘can_export’ => true,
‘has_archive’ => true,
‘exclude_from_search’ => false,
‘publicly_queryable’ => true,
‘rewrite’ => $rewrite,
‘capability_type’ => ‘page’,
);
register_post_type( ‘flow_produkter’, $args );

}
add_action( ‘init’, ‘flow_produkter’, 0 );

}

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer