7

If I am on a product archive page then how can I get current product category ID in product archive page. I am using this but no result:

global $wp_query;
$category_name = $wp_query->query_vars['product_cat'];
$category_object = get_term_by('name', $category_name, 'product_cat');
$category_id = $category_object->term_id;
22

I solve This To get the current category ID.

$cate = get_queried_object();
$cateID = $cate->term_id;
echo $cateID;

and it works like a charm.

2
  • it gives this error: Notice: Undefined property: WP_Post_Type::$term_id in file.php – sheetal Apr 25 '18 at 10:35
  • @Exclutips Nice! Got the job done for me! Thank you! – Ruvee Apr 5 at 2:51
-1

Funciona perfeitamente!

    <?php
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) {
    $product_cat_id = $term->term_id;
    if( $term = get_term_by( 'id', $product_cat_id, 'product_cat' ) ){echo $term->name;}
    break;
}?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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