To make your cpt archive page show a list of your parent terms you can create a template file in your theme with the name archive-{cpt_slug}.php
. This file will automatically be picked up when your cpt archive is called. There instead of writing a loop to output the individual posts you fetch a list of the parent terms to output them. Try to retrieve the terms like this:
$terms = get_terms( array(
'taxonomy' => 'custom_taxonomy',
'parent' => 0,
) );
These terms you link to the respective taxonomy archive page of the terms. For these pages now you create a taxonomy archive template named taxonomy-{custom_taxonomy}.php
. Here you fetch the child terms (change 'parent' => 0,
in the above query to 'parent' => get_queried_object_id(),
). If this yields an empty result just start a loop to output the posts normally, else output the found child terms.
Just keep in mind, that the first archive is the archive for your post type (normally the slug would be: /{cpt_slug}/
), the second template is for the custom taxonomy (here the normal slug would be: /{custom_tax_slug}/{term_slug}/
) and the linked posts are again under the cpt’s slug (normally /{cpt_slug}/{post_slug}/
).
If you need samples how the template files should look, check your current theme’s files an look for the archive.php
and if present taxonomy.php
or category.php
. These files should be a good starting point.