【问题标题】:How to get Categories from product id in single page如何从单页中的产品 ID 获取类别
【发布时间】:2015-07-28 06:40:38
【问题描述】:

我正在尝试从产品 ID 中检索相关类别。 假设我有如下类别。

iPhone (Parent Category)
  -iPhone 3 (sub)
  -iPhone 4 (sub)
  -iPhone 5 (sub)
  -iPhone 6 (sub)

如果是产品:iPhone 4 充电器。 (在 iPhone 4 子类别中)

如何按产品 ID 检索以上所有类别(iPhone 4 充电器) 作为一个列表?

我觉得 Step 可能是 == (get product id) --> (get category) --> (get parent category) --> (get the category list of this parent category)

我正在尝试下面的代码

<?php 
$parent = get_category_parents( $cat, true, ' &raquo; ' ); 
echo $product->get_categories( ', ', '<span>' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); 
?>

【问题讨论】:

标签: php wordpress woocommerce


【解决方案1】:

如果您使用 woocommerce,请尝试以下操作:

<?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');

?>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 2020-12-09
  • 2017-05-26
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多