gpt4 book ai didi

php - 使用 get_the_terms() 不会返回应用于 WooCommerce 产品的特定父类别的子条款

转载 作者:行者123 更新时间:2023-12-04 08:12:00 27 4
gpt4 key购买 nike

经过无数次尝试和大约 50 篇文章后,我得出的结论是这是不可能的,除非有一个巫师愿意从他们的法术书中撕下一页。
在产品页面上时,我希望获取当前产品 ID 并将其传递给 get_the_terms() 函数,同时提供分类法“product_cat”并最终指定父类别。
在我们传递的父类别中,只选择了一个子类别。所以这应该只返回一个术语。
我希望做这样的事情:

$terms = get_the_terms( the_ID(), 'product_cat', array( 'parent' => 84 ) );
foreach ($terms as $term) {
$termID = $term->term_id;
$name = $term->name;
$slug = $term->slug;

echo $name;
}
然而,这会返回很多结果,我尝试了一堆其他类似的查询,但这很容易是最漂亮的,如果有人能透露一些信息,那就太好了!

最佳答案

简单 get_the_terms() 不允许额外的参数,而是使用 wp_get_post_terms() 并且你应该使用 get_the_ID() 而不是 the_ID() 喜欢

$terms = wp_get_post_terms( get_the_id(), 'product_cat', array( 'parent' => 84 ) );
它现在应该更好地工作。

关于php - 使用 get_the_terms() 不会返回应用于 WooCommerce 产品的特定父类别的子条款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65907557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com