permalink for custom post type with custom taxonomy
-
I created custom taxonomy named as “product-category” and custom post type named as “product”
My product category page url : example.com/product-category/term(eg:mobile)
My product single page url : example.com/product/term(eg:iphone 5)But i need,
My product category page url : example.com/term(eg:mobile)
My product single page url : example.com/term(eg:mobile)/term(eg:iphone 5)ie., Similar to wordpress default page permalink structure : example.com/page title/ subpage title
step 1
i added rewrite attribute in both custom taxonomy and custom post type
rewrite=> true,step 2
I installed and activated two plugins
1.http://wordpress.org/support/plugin/custom-post-type-permalinks – for to get heirarical permalink structure (product single page url)
2.http://wordpress.org/plugins/remove-taxonomy-base-slug/ – for to remove custom taxonomy base slug (product category page url)step 3
i added few lines of codes
/**
* Remove the slug from published post permalinks. Only affect our CPT though.
*/
function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( ‘product’ != $post->post_type || ‘publish’ != $post->post_status ) {
return $post_link;
}$post_link = str_replace( ‘/’ . $post->post_type . ‘/’, ‘/’, $post_link );
return $post_link;
}
add_filter( ‘post_type_link’, ‘vipx_remove_cpt_slug’, 10, 3 );/**
* Some hackery to have WordPress match postname to any of our public post types
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* Typically core only accounts for posts and pages where the slug is /post-name/
*/
function vipx_parse_request_tricksy( $query ) {// Only noop the main query
if ( ! $query->is_main_query() )
return;// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query ) || ! isset( $query->query[‘page’] ) ) {
return;
}// ‘name’ will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query[‘name’] ) ) {
$query->set( ‘post_type’, array( ‘post’, ‘product’, ‘page’ ) );
}
}
add_action( ‘pre_get_posts’, ‘vipx_parse_request_tricksy’ );step 4
1. changed custom post type(“product”) permalink settings to %product-category%/%postname%
2. flushed permalink by saving itFirst one: i got exact permalink structure what i expect for custom category page and single page
Second one: Problem is product single page returns : 404 error page . Otherthan else everything is fine.
The topic ‘permalink for custom post type with custom taxonomy’ is closed to new replies.