2

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

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.

2
  • What is the difference to get_the_terms()? Is the object of get_the_id() and the queried object different objects?
    – helle
    Commented Jul 31, 2018 at 11:36
  • 1
    All the the_ and get_the_ functions are for getting data from or related to the current post in the loop. They all refer to the global $post object. On archive pages this could be the first or last post in the list, depending on where you used the function. The 'queried object' represents the main 'thing' that the URL is requesting. On single posts/pages it will be that post/page, on taxonomy archives it will be the requested Term, on post type archives it will be the post type, on Author pages it will be the user, and so on. Commented Jul 31, 2018 at 11:54

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.