0

I want to add a custom slug to permalink for CPT and taxonomy. Like this:

http://mydomain.com/my-custom-slug/post-slug
http://mydomain.com/my-custom-slug/product_category

Here is my CPT and taxonomy code:

$args = array(
    'query_var'           => 'product',
    'capability_type'     => 'post',
    'rewrite'               => array('slug' => 'my-custom-slug'),
);
register_post_type( 'product', $args );

This above CPT product output desired permalink,

http://mydomain.com/my-custom-slug/post-slug

but it give error 404.

and same thing happen with taxonomy:

register_taxonomy('product_category', array('product'), array(
    'hierarchical' => true, 
    'rewrite' => array('slug' => 'my-custom-slug', 'hierarchical' => false, 'with_front' => false)
));

please tell me how do I fix this. Thanks for the help.

1 Answer 1

0

When you register cpt and taxonomies, remove the rewrite argument completely (use the default) and then go to your dashboard, and from Settings > Permalinks set permalink structure to (Custom Structure) and type

/my-custom-slug/%postname%

Screenshots:


The setting:

enter image description here


The result:

enter image description here


save changes, and everything should be just like you want.

3
  • not working, it output: http://mydomain.com/my-custom-slug/%postname%/post-slug Nov 23, 2013 at 10:55
  • Are you sure have you setted everything correctly? (see screenshot I added to answer). %postname% is a rewrite tag embedded in WordPress, not a custom one (see codex.wordpress.org/Using_Permalinks#Structure_Tags), so it should always work. Double check, and if problem persist try to disable your plugin, switch to default theme and try again... @user1983017
    – gmazzap
    Nov 23, 2013 at 11:09
  • Thanks for answer it was helpful, but I want to achieve this from code, not from setting. cuz I want this rewrite rule for a plugin I am working. Thanks for understanding. Nov 23, 2013 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.