0

Is it possible to get a link for a taxonomy as such, not its individual terms? And to which page in the template hierarchy will this link point?

Consider a hierarchical taxonomy called 'products'. Top level terms are 'productgroup1', 'productgroup2' and 'productgroup3'. Now I can get the link of a term like 'productgroup1' (using get_term_link) and it will be something like http://localhost/products/productgroup1, which will point to taxonomy-products.php. On this page I will show child terms of the term 'productgroup1' in the taxonomy.

But I also need a page for showing all top-level terms. So I need a page with link http://localhost/products/ on which I will show those top-level terms 'productgroup1', 'productgroup2' and 'productgroup3'. Also, it would be nice if this link would point to the same template taxonomy-products.php, since most of the code will be the same.

But http://localhost/products/ gives a 404 and a function for getting a link for a taxonomy does not exist? I can only find a function for getting a term's link.

2
  • 1
    Sounds like you need a Custom Post Type called "product" with an archive at "example.com/products" and then you can apply your "productgroup" taxonomy to that CPT.
    – WebElaine
    Commented Nov 30, 2017 at 16:50
  • All archives are a collection of posts, there is no concept of an archive of terms in WordPress. If you just want to force the same template on multiple types of pages, there are template filters that let you do that.
    – Milo
    Commented Nov 30, 2017 at 20:07

1 Answer 1

0

@Webelaine already answered your question.

You're working against yourself - just create a custom post type called product.

Note that custom post types can be hierarchial (like pages), if that's what you're after.

EDIT

Taking into consideration what the OP stated in the comments area, the best advice that I can offer is "create a custom page template" where you query all of the top level taxonomy terms.

The code for that would be:

$args = [
  'taxonomy'     => 'product_category',
  'parent'        => 0,
  'hide_empty'    => false           
];
$terms = get_terms( $args );
// loop through all terms
foreach( $terms as $term ) {
  // display only the term name
  echo '<h4>' . $term->name . '</h4>';
}

Hope that helps

4
  • I already have a custom post type 'product'. When creating a new product entry, the user can select a productgroup from the custom taxonomy to tell what kind of product this is. This is quite normal wordpress behaviour, that is what taxonomies are for, am I right? Using hierarchical custom post types means the hierarchical structure can be redefined with every new product posted, this is error prone. I want the taxonomy to be relatively stable, not to be changed easily or by mistake when entering a new product.
    – Michiel
    Commented Nov 30, 2017 at 18:44
  • Ah, got confused when you said "Consider a hierarchical taxonomy called 'products'." A taxonomy would be "product tags" or "product category". Let me modify the answer accordingly. Commented Nov 30, 2017 at 19:02
  • Yes, that's the code that I use in taxonomy-products.php. As I understand you advise to make another file (a page template) to show the top-level terms in. The thing is, both files will contain roughly the same code, since both files will query the taxonomy to show the terms with the same html. I was hoping on a more elegant solution, I don't like to duplicate code. Of course I could place the code in another file and include it in both files, but I was hoping on a more elegant solution.
    – Michiel
    Commented Nov 30, 2017 at 19:38
  • Well you could always include that code at the very top of the archives page of said post type. So you'd basically create archive-product.php (assuming "product" is your cpt). Then have that code at the very top to display only the taxonomies and then have the regular loop underneath that. Commented Nov 30, 2017 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.