Custom post type url path not showing taxonomy

Black Sulfur
Black Sulfur used Ask the Experts™
on
I have a custom taxonomy which has about 3 levels, parent, child, grandchild which is visible in the URL when you drill down eg:

example.com/my-cagegory/parent/child/grandchild/

Open in new window


And then after grandchild you get to the actual post type. But, I was hoping the url would be:

example.com/my-cagegory/parent/child/grandchild/my-product

Open in new window

But instead it is:

example.com/products/my-product
So, I am losing that whole taxonomy path.

function my_post_types() {

        register_post_type('product', array(
        'supports'      => array('title', 'editor', 'thumbnail', 'excerpt'),
        'rewrite'       => array('slug' => 'products'),
        'has_archive'   => true,
        'public'        => true,
        'labels'        => array(
                'name'         => 'Products',
                'add_new_item' => 'Add New Product',
                'edit_item'    => 'Edit Product', 
                'all_items'    => 'All Products',
        ),
        'menu_icon'     => 'dashicons-cart'
    ));
}


add_action('init', 'my_post_types');

function my_product_taxonomy() {

    $labels = array(
        'name' => _x( 'Product Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Product Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Categories' ),
        'all_items' => __( 'All Categories' ),
        'parent_item' => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item' => __( 'Edit Category' ), 
        'update_item' => __( 'Update Category' ),
        'add_new_item' => __( 'Add New Category' ),
        'new_item_name' => __( 'New Category Name' ),
        'menu_name' => __( 'Product Categories' ),
    );

    register_taxonomy('productCategory',array('product'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'hierarchical' => true, 'slug' => 'product-category' ),
  ));   
}

 add_action('init', 'my_product_taxonomy', 0);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Guru
Most Valuable Expert 2011
Commented:
It looks like this can be a little hairy to resolve, and requires some URL rewriting. There's some information on a possible solution here, which includes a couple of links near the bottom of the answer, including this one: https://themoonwatch.com/en/2012/custom-permalinks-hierarchical-taxonomies/#sthash.6UaovMFc.pYu910U5.dpbs

If nobody else suggests anything better, that might help anyway.

Author

Commented:
Thanks Terry, yeah, it is quite a difficult thing (well, for me anyway). Before your answer I found a plugin that helps:

https://en-gb.wordpress.org/plugins/custom-post-type-permalinks/https://en-gb.wordpress.org/plugins/custom-post-type-permalinks/

But the issue I have is that the path up until the product changes once you hit the actual product-single.php

So, up until that point it is:

example.com/my-category/parent/child/grandchild/

Open in new window


Then it changes to:

example.com/product/parent/child/grandchild/my-product

Open in new window


So, it changes 'my-category' to 'product'. Not sure if that is because of the way I have set up my custom post type/taxonomy or what do you think there?
Terry WoodsIT Guru
Most Valuable Expert 2011

Commented:
A quick comment - Rewrite rules at the web server level are a possible option at least. That could involve editing the .htaccess file for the site.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial