categories - Custom Post Type Archive Page Filtering

I have a custom post type with a hierarchical custom taxonomy. The taxonomy has several parent terms and each parent ter

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
  • Following your description, if you have a post which is only assigned to a parent term which has child terms, but the post isn't assigned to any child terms, the post will not appear on any archive page. – JHoffmann Commented Jan 26, 2017 at 23:40
  • @JHoffmann This won't happen. It wouldn't be logical, because the nature of our website. If there are child terms, the posts have to be assigned to them. – Sam Commented Jan 26, 2017 at 23:45
  • I just wanted to raise that point in case you were not planning to prevent that. – JHoffmann Commented Jan 26, 2017 at 23:55
  • @JHoffmann No problem :) – Sam Commented Jan 26, 2017 at 23:57
  • I tried to register a post type and a taxonomy sharing a common base slug, for me this didn't work (I got a 404 for the taxonomy archive). Having the terms show up under 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
 |  Show 1 more comment

1 Answer 1

Reset to default 1

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744787873a4593736.html

相关推荐

  • categories - Custom Post Type Archive Page Filtering

    I have a custom post type with a hierarchical custom taxonomy. The taxonomy has several parent terms and each parent ter

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信