Home WordPressWooCommerce How to get product category in Woo-commerce?

How to get product category in Woo-commerce?

by admin
0 comment 94 views

To get Category of the product is very easy. We have to get all categories related to Woo-commerce products. To do this WordPress function get_terms( ) is used. Following detail of this WordPress function is given:

<?php 
      get_terms( string|array $args = array(), array $deprecated = '' ); 
?>

So in get_terms accept string or array in first argument. To get all categories related to posts we put post_tag, But we have to take all categories related to products so instead of ‘post_tags‘ we will insert ‘product_cat‘.

<?php 
$terms = get_terms( 'post_tag', array(
    'hide_empty' => false,
) ); 
?>
<?php 
    $terms = get_terms( 'product_cat' ); 
?>

By using this argument we get all categories related to product categories. We will get array of all categories.

 

 

Leave a Comment

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy