admin管理员组

文章数量:1122832

I have the ID from a product (1345) how can i get the category name of that specific product?

I try

$post_categories = wp_get_post_categories( $post->ID );
print_r( $post_categories );

but it outputs:

Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

What those it means ?

Thanks

I have the ID from a product (1345) how can i get the category name of that specific product?

I try

$post_categories = wp_get_post_categories( $post->ID );
print_r( $post_categories );

but it outputs:

Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

What those it means ?

Thanks

Share Improve this question edited Nov 23, 2012 at 15:42 kaiser 50.8k27 gold badges150 silver badges244 bronze badges asked Nov 23, 2012 at 15:30 Rodrigo SanzRodrigo Sanz 4852 gold badges4 silver badges12 bronze badges 2
  • 2 What does the ID represent? What is a 'product'? Is this a custom post type? A meta field? What? – s_ha_dum Commented Nov 23, 2012 at 15:37
  • 1 Hi, It's a woocommerce product and it's a post type. – Rodrigo Sanz Commented Nov 23, 2012 at 19:35
Add a comment  | 

5 Answers 5

Reset to default 14

Since the question is tagged woocommerce, i'm assuming that it's a product CPT created by woocommerce wordpress plugin. This answer doesn't apply if that's not the case.

The products categories is not normal categories, they are a custom taxonomy created specifically for products which is just labeled as "Categories".

You should go through the woocommerce documentation to find some function that would do this for you, if you don't find anything you can try an alternative solution. For that, first you should know the name of the taxonomy. You can copy it from inside the url in your browser when you visit categories edit screen in the backend. Then you can use wp_get_post_terms to get the terms.

Option #1

Get all product_cat's using this function

global $product;

$terms = get_the_terms( $product->get_id(), 'product_cat' );

Offical docs


Option #2 If you only need their ids, you can get all product_category_ids associated with a specific product, using this function:

global $product;

$product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );

Official docs


Extra

If you would like to print out - for instance - the categories names, you need the category term-object. This can be retrieved using get_term_by().

An example:

foreach( $product_cats_ids as $cat_id ) {
    $term = get_term_by( 'id', $cat_id, 'product_cat' );

    echo $term->name;
}

I answered my own question, this work for me :

<?php 
$term_list = wp_get_post_terms($id_product,'product_cat',array('fields'=>'ids'));
$cat_id = (int)$term_list[0];
echo get_term_link ($cat_id, 'product_cat');

?>

Thanks Mridul Aggarwal for your help

<?php
   $terms = get_the_terms($product->ID, 'product_cat');
       foreach ($terms as $term) {

         $product_cat = $term->name;
            echo $product_cat;
              break;
          }
   ?>

Get all product_cat's & output

global $product;
$product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );
foreach( $product_cats_ids as $cat_id ) {
    $term = get_term_by( 'id', $cat_id, 'product_cat' );
    echo $term->name;
}

本文标签: pluginsGet the category from an ID of a product