I used a custom post type in my website: I added archive-{mytaxonomy}.php
and also single-{mytaxonomy}.php
.
I also created a separate sidebar for my taxonomy and everything looks fine until I click on one of my categories. Then it loads archive.php
.
Should I add any other code to my files?
Categories and Taxonomies are not exactly the same.
A category is a default taxonomy
You can use categories with custom post types however its better to use custom taxonomies because the truth is, they are an extremely powerful way to group various items in all sorts of ways.
If you’ve added code like above which adds the option to create custom taxonomy types, then you’ll need to create a file named something like:
Where cpt is the name of your custom post type.
You can use the code above in your child themes functions file and replace all instances of cpt and CPT with the name of your custom post type.
You would also need to add this line to the code which registers your custom post type:
Here’s a working example:
Again, replace all instances of cpt and CPT with the name of your custom post type.
For custom post type archive pages, use something like this:
For custom post type single pages, use something like this:
Again, replace cpt in the file name with the name of your custom post type.
To get custom post type posts with specific category use custom taxonomy
Register the taxonomy name of the custom post type like categories and then assign a category to each post when you added new post.
Here is the example of the code for custom post type news and its taxonomy news_category
then
create taxonomy template page ‘taxonomy-news_category.php’
and add the query to get the posts with this category name
That’s it.
If you are using “Custom Post Types UI” plugin to make custom post types make sure you have changed Has Archive option to True, if you made your custom post type manually make sure you have added “‘has_archive’ => true” to the array.
Then, lets say you added a new custom post type called “Movies”, and then you added a new taxonomy linked to it which slug is called “genres” and added a new term to Genres which slug is called “horror”,and you wanted to make a specific template for “Horror”, then you can make a template which named “taxonomy-{taxonomy}-{term}.php”.
In this case the template should be named taxonomy-genres-horror.php