I use Woocommerce in my theme and was wondering how to get the unique category, when checking terms.

what I did so far:

  1. I enabled the theme supprt add_theme_support( 'woocommerce' ); in
    the functions php.
  2. I created a folder woocommerce and a file archive-product.php
  3. The file is reached for the url `example.com/product-category/categoryname1/
  4. I use this code to get the categroies from woocommerce products: $terms = get_the_terms(get_the_ID(), 'product_cat');

The thing is, that I get more than one categroy. Namely all of the first product (post) in my product list (starting with letter “A..” and allocated for the two main and two subcategories).

I would like to get only the categoryname1 entry.

What did I do wrong?

Has this something to do with the special permalink configuration for the category-base?

1 Answer
1

On category archives (or any the archive for any taxonomy term) you can get the current term with get_queried_object().

If you just need the ID you can use get_queried_object_id().

If you want to output the name of the term, you can use single_term_title().

Keep in mind that archive-product.php will also be used for the Shop page, for all products, so make sure to check is_product_category() before using any of the above functions.

Leave a Reply

Your email address will not be published. Required fields are marked *