0

I have a custom post type products and I have a custom taxonomy 'product_Range`.

I would like the url structure to be as follows:

www.mysite.com/products/

www.mysite.com/products/%category%

www.mysite.com/products/%category%/%category%/%category%/%post_name%

At the moment when I get to this page www.mysite.com/products/%category%/%category%/%category%/ the page 404s and does not display the post.

I found this helpful question on SO (Permalinks: custom post type -> custom taxonomy -> post) but seem to be missing something, does anybody know how I can add the category base to the URL?

Here is a snippet of my custom taxonomy code:

'rewrite' => array(
     'slug' => 'products',
     'with_front' => false,
     'hierarchical' => true
)

Here is a snippet of my custom post type code:

$args = array(
    'labels'                => $labels,
    'public'                => true,
    'rewrite'               => array('slug' => 'products/%product_range%'),
    'has_archive'           => 'products',
    'supports'              => array( 'title', 'editor', 'thumbnail', 'page-attributes')
);

Here is the function to re-write the slug:

function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'product' ){
    $terms = wp_get_object_terms( $post->ID, 'product_range' );
    if( $terms ){
        return str_replace( '%product_range%' , $terms[0]->slug , $post_link );
    }
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.