Woocommerce: Get Current Product Category

How to get the current product category that the user is viewing?

I am trying to use get_the_terms($post->ID, 'product_cat'); but it gives me categories for each product listed on the page. I would like the page in the current category to view the current page in the product list.

+7
woocommerce
source share
3 answers

Here is one liner:

 $wp_query->get_queried_object()->term_id; 

or

 $wp_query->get_queried_object()->name; 

or

 ... 
+8
source share

try the following:

  global $post; $terms = get_the_terms( $post->ID, 'product_cat' ); $nterms = get_the_terms( $post->ID, 'product_tag' ); foreach ($terms as $term ) { $product_cat_id = $term->term_id; $product_cat_name = $term->name; break; } echo $product_cat_name; 
+4
source share

Get the current category identifier. you should use

 get_queried_object(); 

The correct way to do this is:

 $cate = get_queried_object(); $cateID = $cate->term_id; echo $cateID; 
+1
source share

All Articles