Php woocommerce-如何获得当前产品类别中最顶级的类别
我有一个Woocommerce产品,我需要在该页面上显示指定给该产品的类别中最顶级的类别Php woocommerce-如何获得当前产品类别中最顶级的类别,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有一个Woocommerce产品,我需要在该页面上显示指定给该产品的类别中最顶级的类别 - Main Product Category -- Sub Product Category --- Category Assigned to product 我需要获取“主要产品类别”的ID或名称,以便在单个产品类别中显示它 我已经尝试了以下操作: global $post; $terms = get_the_terms( $post->ID, 'product_cat' ); foreach
- Main Product Category
-- Sub Product Category
--- Category Assigned to product
我需要获取“主要产品类别”的ID或名称,以便在单个产品类别中显示它
我已经尝试了以下操作:
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
$thetop_parent = woocommerce_get_term_top_most_parent( $product_cat_id , 'product_cat' );
echo $thetop_parent;
}
但它根本不起作用,在woocomerce_get_term之后,它阻止页面加载。。。我不知道在这一点上该怎么做
谢谢你在这方面的帮助。经过大量研究,我找到了解决这个问题的方法。我希望这能帮助别人 解决方案:
global $post;
$prod_terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($prod_terms as $prod_term) {
// gets product cat id
$product_cat_id = $prod_term->term_id;
// gets an array of all parent category levels
$product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );
// This cuts the array and extracts the last set in the array
$last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
foreach($last_parent_cat as $last_parent_cat_value){
// $last_parent_cat_value is the id of the most top level category, can be use whichever one like
echo '<strong>' . $last_parent_cat_value . '</strong>';
}
}
global$post;
$prod_terms=get_the_terms($post->ID,'product_cat');
foreach($prod\u期限作为$prod\u期限){
//获取产品类别id
$product\U cat\U id=$prod\U term->term\U id;
//获取所有父类别级别的数组
$product_parent_categories_all_hierachy=get_祖先($product_cat_id,'product_cat');
//这将剪切数组并提取数组中的最后一个集
$last\u parent\u cat=array\u slice($product\u parent\u categories\u all\u hierarchy,-1,1,true);
foreach($last\u parent\u cat作为$last\u parent\u cat\u值){
//$last\u parent\u cat\u值是最高级别类别的id,可以使用任意一个
回显“”.$last_parent_cat_value。”;
}
}
或者可能是:
$cat = get_the_terms( $product->ID, 'product_cat' );
foreach ($cat as $categoria) {
if($categoria->parent == 0){
echo $categoria->name;
}
}
我知道这是一篇老文章,但我有一个类似的情况,我们需要获得当前正在查看的产品的根类别。我能想到的最简单的解决方案就是看看WooCommerce是如何制作面包屑的,这段代码就是我的诀窍:
if ( is_product() ) {
global $post;
$terms = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) );
if ( ! empty( $terms ) ) {
$main_term = $terms[0];
$ancestors = get_ancestors( $main_term->term_id, 'product_cat' );
if ( ! empty( $ancestors ) ) {
$ancestors = array_reverse( $ancestors );
// first element in $ancestors has the root category ID
// get root category object
$root_cat = get_term( $ancestors[0], 'product_cat' );
}
else {
// root category would be $main_term if no ancestors exist
}
}
else {
// no category assigned to the product
}
}
如果您有“类别\标识”,请点击此处
这里是我实际使用的解决方案
当然很高兴它帮助了某人。我花了一段时间才解决这个问题:)最后我到处寻找这个,先生,你是救世主!更优化的代码。它应该被标记为答案。我在测试时尝试过,但由于模板的复杂性和高度定制,它无法工作。这就是为什么我提出了漫长的道路,它在任何可能的方式工作。你的方法是我尝试的第一件事,但没有成功。我对woocommerce并不陌生,它之所以这么长是有原因的。这种方法不会像它需要的那样沿着祖先链向上传播。不起作用。伙计,那是美丽、简单和优雅的,我现在高兴得哭了。非常感谢。如果有人不知道如何应用它,可以通过
functions.php
添加此函数。该函数接受wp_对象,看在上帝的份上!!!再次谢谢你D
/*If you dont have a category ID use this:
* $category_ID = get_queried_object()->term_id;
* This will get you the current category_ID
*/
$termid = get_term($category_ID, 'product_cat' );
if($termid->parent > 0)
{ // get the parent's hierarchy.
$cat_hierachy = get_ancestors( $category_ID, 'product_cat' );
return end($cat_hierachy); // returns the Level-1 category_ID
}
return $category_ID; // Has no parent so return THIS category_ID Level-1
function get_parent_terms($term) {
if ($term->parent > 0){
$term = get_term_by("id", $term->parent, "product_cat");
return get_parent_terms($term);
}else{
return $term->term_id;
}
}
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$Root_Cat_ID = get_parent_terms($cat_obj);