问题描述
经过无数次尝试和大约 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 ) );
现在应该更好用了。