I have a custom post type with a hierarchical custom taxonomy. The taxonomy has several parent terms and each parent term has several child terms. The posts are assigned to one parent and (mostly) one child.
I want that my archive page example/{cpt_slug}
only displays the parent terms. After a first selection, the user will be on the second archive page example/{cpt_slug}/{parent_term_sug}
. If the selected term has children, this page should only display those, so that the user can, once again, take a decision on what he wants to see. Otherwise, the posts of this term should be shown.
At the moment there are no plans to have child-child-terms, but if it's possible to have a completely dynamic code which could handle grandchildren and maybe even more, that would be even better.
Have you any idea on how I could achieve this? Many thanks in advance!!
I have a custom post type with a hierarchical custom taxonomy. The taxonomy has several parent terms and each parent term has several child terms. The posts are assigned to one parent and (mostly) one child.
I want that my archive page example/{cpt_slug}
only displays the parent terms. After a first selection, the user will be on the second archive page example/{cpt_slug}/{parent_term_sug}
. If the selected term has children, this page should only display those, so that the user can, once again, take a decision on what he wants to see. Otherwise, the posts of this term should be shown.
At the moment there are no plans to have child-child-terms, but if it's possible to have a completely dynamic code which could handle grandchildren and maybe even more, that would be even better.
Have you any idea on how I could achieve this? Many thanks in advance!!
Share Improve this question asked Jan 26, 2017 at 23:30 SamSam 4356 silver badges18 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1To 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744787873a4593736.html
example/{cpt_slug}/{parent_term_sug}
urls is probably not going to be easy to implement. Maybe with a plugin like Custom Post Type Permalinks (I haven't tested this). – JHoffmann Commented Jan 27, 2017 at 0:27