CPT taxonomy url structure
-
Hi there,
i have created a custom post type, named “products” and a taxonomy named “products_category”:
add_action('init', function () { register_taxonomy( 'products_category', 'products', array( 'labels' => array(...), 'hierarchical' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'products/category', 'with_front' => false ) ) ); register_post_type('products', array( 'labels' => array(....), 'public' => true, 'publicly_queryable' => true, 'query_var' => true, 'rewrite' => true, 'hierarchical' => true, 'has_archive' => true, 'show_in_rest' => true, 'exclude_from_search' => false, 'supports' => array('title'), 'rewrite' => array('slug' => 'products'), )); });
The current url structure is:
- products (archive/list of products -> use the archive-products.php template)
- products/name_of_product (single view of a product -> use the single-products.php template)
- products/category/name_of_category (list of products assigned to the category -> use the taxonomy-products_category.php template)
This works fine and out of the box. My question is: Can i restructure this to “products/name_of_category” + “products/name_of_product”. My idea is to check if the term is in array like the pseudo code below and return the products assigned to the category if a category is given in the url, otherwise i return the normal single product.
$categories = get_terms('products_category', array('hide_empty' => false, 'fields' => 'slugs')); in_array(get_query_var('products-category'), $categories) ...
Thanks for the help
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘CPT taxonomy url structure’ is closed to new replies.