I want to create a custom taxonomy which behaves similar to the post type as a category behaves to a the default posts (on the grounds of /%category%/%postname%/ permalink structure) so that the posts in the custom post types are displayed as
www.example.com/custom-post-type/custom-taxonomy-name/post-name
Also I want the category meta box to appear only when we add new default post and not when we add a new post in the custom post type and the custom taxonomy box to appear only when we add a new post in the custom post type and not when we add new a default post.
Leave a Reply
You must be logged in to post a comment.
First of all, if you want to show taxonomy metabox only to your custom post type, then register the taxonomy to only that custom post type by passing the custom post type name as argument in the
register_taxonomy()
function. By doing this, the taxonomy metabox appears only to custom post type. If you don’t want to show category metabox to your custom post type, then remove the term category as an argument while registering your custom post type and instead include the taxonomy slug name like this:'taxonomies' => array( 'post_tag', 'your_taxonomy_name')
. Here is the code of how I achieved that.I have registered a custom taxonomy with slug “themes_categories” under under custom post type themes:
Then to change the permalink, I have created the following function:
Then I registered a custom post type with slug “themes” as below:
There are few things you have to remember while registering custom post. Change the “has_archive” parameter to the custom post type slug name and the rewrite slug name as
'slug' => 'custom_post_type_slug/%taxonomy_slug%
.Now when you add a new post type in the right post type page, you will see the permalink as
http://www.example.com/wordpress/themes/%themes_categories%/post-name/
. If the custom taxonomy for this post is not selected, the permalink will remainhttp://www.example.com/wordpress/themes/%themes_categories%/post-name/
, which will then show a bad request.To correct this, we create a default term in the custom taxonomy (same as uncategorized in categories).
Add this to functions.php:
Now when the custom taxonomy is left blank. the permalink becomes
http://www.example.com/wordpress/themes/other/post-name/
automatically.Lastly, don’t forget to flush the rewrite by clicking on the “Save Changes” button in the permalink setting of the WP backend admin section, otherwise you will be redirected to 404 error. I hope this helps you.
i.e. register a custom taxonomy
MY_NEW_CARSS
for custom post types: