wordpress 如何从woocommerce的id产品中获取类别名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22838939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:31:03  来源:igfitidea点击:

How to get the category name from an id product in woocommerce

wordpresswoocommerce

提问by user2644743

I am creating a e-commerce website using woo commerce. I am struggling with a code that how to display the category name of the product using category id in woocommerce?.

我正在使用 woo commerce 创建一个电子商务网站。我正在为如何在 woocommerce 中使用类别 id 显示产品的类别名称的代码而苦苦挣扎。

In wordpress its easy to display the category name by id as get_the_category_by_id(3). So that it display the name of the id 3.

在 wordpress 中,很容易按 id 显示类别名称为get_the_category_by_id(3)。以便它显示 id 3 的名称。

By the same way how to display the category name by id in woocommerce?

同样的方式如何在woocommerce中按id显示类别名称?

回答by Ratnakar - StoreApps

Try this function to get product category name

试试这个功能来获取产品类别名称

function get_product_category_by_id( $category_id ) {
    $term = get_term_by( 'id', $category_id, 'product_cat', 'ARRAY_A' );
    return $term['name'];
}
$product_category = get_product_category_by_id( $your_category_id );

Hope this will be useful.

希望这将是有用的。

回答by Yogesh Pawar

you can use something like this

你可以使用这样的东西

$product_cats = wp_get_post_terms( your_id, 'product_cat' );

Please, let me know whether it helps.

请让我知道它是否有帮助。

print_r($product_cats);

回答by Amritosh pandey

You can use wp_get_post_terms to get category id of any product like this :

您可以使用 wp_get_post_terms 来获取任何产品的类别 ID,如下所示:

$term_list = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));

I am just fetching id's of categories assigned to the particular product, You can fetch name also.

我只是获取分配给特定产品的类别 ID,您也可以获取名称。

Hope this will be useful.

希望这将是有用的。