I have registered a custom post type [equipment] and have a taxonomy of [equipment_type] within the taxonomy I have parent and child categories. For example:
Equipment (Custom post type)
Equipment Types (Taxonomy)
Cameras (Parent term)
-
Camera A (Child term)
-
Camera B
What I would like to create is effectively an archive page for the taxonomy terms. So when either ‘Cameras’ or ‘Camera A’ is selected it shows say 12 posts with title and featured image (links to single post) plus some pagination.
I have tried a standard WP query and Loop and it always ends up showing all of the taxonomies posts in all terms.
I currently have a taxonomy-equipment_types.php
template set up to handle the query.
I want to document this because I just found the answer recently.
The problem with having taxonomy is that most developers have the mindset of expecting the taxonomy to be seen inside the
post_type
url of:Instead, you are going to find the url in:
This means that we often may be creating the template correctly as
But the right way of using it is to expect it inside the url
To view the correct url for the taxonomy, we can use
And test wherever the link is going to point to.
The WordPress Template Hierarchy provides the exact template file that you need:
taxonomy-{taxonomy}-{term}.php
.So, to create a custom template for the
cameras
term of theequipment_types
taxonomy, you would create a file namedtaxonomy-equipment_types-cameras.php
.(Note, you can also create a template file for the taxonomy itself; just omit the
{term}
slug:taxonomy-{taxonomy}.php
, ortaxonomy-equipment_types.php
in your case.)You can conditionally output content based on hierarchy by querying for either the term parent, via the object properties returned by
get_term()
, or the term children, viaget_term_children()
.I had a similar issue. The problem with the above answers is that they all require you to specify the CPT, the taxonomy or the term.
If – as you’ve indicated – you want to retrieve this dynamically depending on which CPT page the user is on, you can try the following (which works for me), which displays all the taxonomies for the current custom post type.
(originally from this post with the help of GhostToast)
Save the above in a file called something like archive-mycpt.php, then in archive.php add this after the header call;