Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

Custom post type url path not showing taxonomy

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

ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Crazy Horse

ASKER

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?
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.