我想根据当前类别 ID 检索术语 ID 列表。
目前我正在使用以下代码:
$product_cat_items = get_queried_object();
$product_cat_id = $product_cat_items->term_id;
$product_cat_child = get_term($product_cat_id, 'product_cat');
$product_cat_parent = $product_cat_child->parent;
$product_cat_related= get_terms('product_cat', array( 'parent' => $product_cat_parent, 'exclude' => $product_cat_id ));
它正在工作,我得到了一系列术语。
但问题是,我只需要术语对象中的 ID 即可获取如下列表:
123,345,678
有没有办法从列表中提取这样的列表$product_cat_related
array?
这是当前的输出:
array(2) {
[0]=>
object(WP_Term)#26238 (10) {
["term_id"]=>
int(177)
["name"]=>
string(27) "Name"
["slug"]=>
string(21) "name"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(177)
["taxonomy"]=>
string(11) "product_cat"
["description"]=>
string(0) ""
["parent"]=>
int(140)
["count"]=>
int(8)
["filter"]=>
string(3) "raw"
}
[1]=> ....
}
自 WordPress 版本 4.5.0 起,分类法应通过“taxonomy”参数传递$args
array (see get_terms()文档).
Also get_queried_object()
已经给出了WP_Term
当查询的对象是分类术语时的对象。
您也可以使用'fields' => 'ids'
作为一个论点get_terms()
,只得到一个数组term Ids而不是数组WP_term
对象(see WP_Term_Query可用参数).
最后,您将使用 PHPimplode()
获取一串逗号分隔的术语 Id。
所以你的代码将是:
$current_term = get_queried_object(); // Already a WP_Term Object
if ( $current_term->parent > 0 ) {
$siblings_ids = get_terms( array(
'taxonomy' => 'product_cat',
'parent' => $current_term->parent,
'exclude' => $current_term->term_id,
'fields' => 'ids',
) );
// Get a string of coma separated terms Ids
$siblings_list_ids = implode(',', $siblings_ids);
// Testing output
echo $siblings_list_ids;
}
经过测试并有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)