如何解决使用产品ID或类别ID从WooCommerce产品获取类别名称
我试图获取产品的类别名称(例如“营养”,“运动”等),但是我不能。
我正在尝试这种方式:
$test = wc_get_product_category_list(1202);
但是我得到的是以下内容:
<a href="https://example.es/product-category/nutricion/sustitutivos/nutricion-batidos/" rel="tag">Batidos</a>,<a href="https://example.es/product-category/nutricion/" rel="tag">Nutrición</a>
我只需要标签“ Batidos”,“Nutrición” ...
我将非常感谢您的帮助,我已经尝试了很长时间,但是我不能...
致谢和感谢。
解决方法
要从产品ID获取产品类别名称,请改用wp_get_post_terms()
函数:
$product_id = 1202; // product Id
$term_names_array = wp_get_post_terms( $product_id,'product_cat',array('fields' => 'names') ); // Array of product category term names
$term_names_string = count($term_names_array) > 0 ? implode(',',$term_names_array) : ''; // Convert to a coma separated string
echo $term_names_string; // Display the string
要从产品类别ID获取产品类别名称,请使用get_term-by()
函数,如下所示:
$term_id = 125; // Product category term Id
$term = get_term-by( 'term_id',$category_term_id,'product_cat' ); // The WP_Term Object
$term_name = $term->name; // Get the term name from the WP_Term Object
echo $term_name; // Display
经过测试可以正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。