从产品ID获取类别?
-
-
ID代表什么?什么是"产品"?这是自定义帖子类型吗?元字段?什么?What does the ID represent? What is a 'product'? Is this a custom post type? A meta field? What?
- 2
- 2012-11-23
- s_ha_dum
-
嗨,这是woocommerce产品,它是帖子类型.Hi, It's a woocommerce product and it's a post type.
- 0
- 2012-11-23
- Rodrigo Sanz
-
4 个回答
- 投票数
-
- 2012-11-23
由于该问题被标记为woocommerce,因此我假设它是woocommerce wordpress插件创建的CPT产品.如果不是这种情况,则此答案不适用.
产品类别不是常规类别,它们是专门为产品创建的自定义分类法,仅被标记为"类别".
您应该浏览woocommerce文档,以找到可以为您执行此操作的功能,如果您找不到任何内容,则可以尝试其他解决方案.为此,首先您应该知道分类法的名称.当您访问后端的类别编辑屏幕时,可以从浏览器的url内部复制它.然后,您可以使用
wp_get_post_terms
来获取条款.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.-
嗨,谢谢您的回答.是的,是wordpress中的woocommerce产品.好的,我要尝试一下,wp_get_post_terms的$ args是多少?我看到"分类法"和"参数"是可选的,因此我将仅尝试使用ID.Hi, thanks for your answer. And yes is a woocommerce product in wordpress. Ok, i'm going to try it, what are the $args for wp_get_post_terms ? i see the "taxonomy" and "args" are optional, so i'm going to try only with the ID.
- 0
- 2012-11-23
- Rodrigo Sanz
-
它仅适用于ID.默认分类法是`post_tag`.您需要在此处传递分类法的名称.它可以在没有$ args的情况下工作,但是您可以根据需要使用它.它的目的是按照[本页](http://codex.wordpress.org/Function_Reference/wp_get_object_terms)中的说明覆盖默认值.It won't work only with ID. The default taxonomy is `post_tag`. You need to pass the name of the taxonomy there. It will work without `$args` but you can use it if you want. It's meant to override the defaults as explained on [this page](http://codex.wordpress.org/Function_Reference/wp_get_object_terms)
- 0
- 2012-11-24
- Mridul Aggarwal
-
我进行了测试,但没有输出产品所属的类别.我使用了这个," all")); print_r($term_list); ?>I tested but it doesn't output the categiry that the product is in. I used this, "all")); print_r($term_list); ?>
- 0
- 2012-11-25
- Rodrigo Sanz
-
它输出这个:Array([0]=> stdClass Object([term_id]=> 104 [name]=>new [slug]=>new [term_group]=> 0 [term_taxonomy_id]=> 104 [taxonomy]=>product_tag[description]=> Holaquétalestoes una unadescripción?[parent]=> 0 [count]=> 8))Array([0]=> stdClass对象([term_id]=> 104 [name]=>新[子弹]=>新的[term_group]=> 0 [term_taxonomy_id]=> 104 [分类]=>product_tag [描述]=> Hola quetalestoesesnotescripción?[parent]=> 0 [count]=> 8))it outputs this :Array ( [0] => stdClass Object ( [term_id] => 104 [name] => new [slug] => new [term_group] => 0 [term_taxonomy_id] => 104 [taxonomy] => product_tag [description] => Hola qué tal esto es una descripción? [parent] => 0 [count] => 8 ) ) Array ( [0] => stdClass Object ( [term_id] => 104 [name] => new [slug] => new [term_group] => 0 [term_taxonomy_id] => 104 [taxonomy] => product_tag [description] => Hola qué tal esto es una descripción? [parent] => 0 [count] => 8 ) )
- 0
- 2012-11-25
- Rodrigo Sanz
-
而且数组中没有类别?我做错什么了吗?And there's not category in the array ? Did i do something wrong?
- 0
- 2012-11-25
- Rodrigo Sanz
-
您使用了"product_tag".类别名称是否可能是"product_category"?代码看起来不错,从输出中您将获得一个名为"new"且ID为104的术语."new"是其中一个标签的名称吗?You used `product_tag`. Is it possible that the category name was `product_category`? The code seems fine & as from the output you're getting a term named `new` with an id of 104. Is `new` the name of one of the tags?
- 0
- 2012-11-25
- Mridul Aggarwal
-
- 2017-09-02
选项1
使用此功能获取所有product_cat的
global $product; $terms = get_the_terms( $product->get_id(), 'product_cat' );
选项#2 如果只需要它们的ID,则可以使用以下功能获取与特定产品关联的所有product_category_id:
global $product; $product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );
额外
例如,如果您想打印出类别名称,则需要类别术语对象.可以使用
get_term_by()
进行检索.一个例子:
foreach( $product_cats_ids as $cat_id ) { $term = get_term_by( 'id', $cat_id, 'product_cat' ); echo $term->name; }
Option #1
Get all product_cat's using this function
global $product; $terms = get_the_terms( $product->get_id(), 'product_cat' );
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' );
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; }
-
- 2012-11-27
我回答了我自己的问题,这对我来说是有用的:
<?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'); ?>
感谢Mridul Aggarwal的帮助
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
-
如果您认为Mridul的答案不够完整,不能被接受为正确答案,那么您至少可以对它作出回答.显然,它使您走上了正确的道路.You could at least upvote Mridul's answer, if you don't think it's complete enough to be accepted as correct. Clearly it got you on the right track.
- 3
- 2012-11-27
- Johannes Pille
-
- 2020-08-19
<?php $terms = get_the_terms($product->ID, 'product_cat'); foreach ($terms as $term) { $product_cat = $term->name; echo $product_cat; break; } ?>
<?php $terms = get_the_terms($product->ID, 'product_cat'); foreach ($terms as $term) { $product_cat = $term->name; echo $product_cat; break; } ?>
-
请** [编辑]您的答案**,并添加说明:**为什么**可以解决问题?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2020-08-19
- fuxia
我从产品(
ID
)中获得了1345
,我如何获得该特定产品的类别名称?我尝试
但它输出:
这是什么意思?
谢谢