For example :
- A = 21
- B = 22
- C = 23
如何使用23个子ID获得21和22个ID?
更新(2020)
要从产品类别术语ID获取父术语ID,请尝试以下操作(带注释的代码):
// Get the parent term slugs list
$parent_terms_list = get_term_parents_list( 23, 'product_cat', array('format' => 'slug', 'separator' => ',', 'link' => false, 'inclusive' => false) );
$parent_terms_ids = []; // Initialising variable
// Loop through parent terms slugs array to convert them in term IDs array
foreach( explode(',', $parent_terms_list) as $term_slug ){
if( ! empty($term_slug) ){
// Get the term ID from the term slug and add it to the array of parent terms Ids
$parent_terms_ids[] = get_term_by( 'slug', $term_slug, 'product_cat' )->term_id;
}
}
// Test output of the raw array (just for testing)
print_r($parent_terms_ids);
经过测试和工作。
加成:
您可以更好地使用Wordpressget_ancestors()
专用功能,例如在此最新答案线程中或在其他相关答案中。
在这种情况下,代码将是:
// Get the parent term ids array
$parent_terms_ids = $parent_ids = get_ancestors( $child_id, 'product_cat' , 'taxonomy');
// Test output of the raw array (just for testing)
print_r($parent_terms_ids);
相关的踏板:
记载的相关Wordpress功能:
get_term_parents_list()
get_term_by()
get_ancestors()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句