我使用WordPress的get_terms返回一个WP_Term对象数组,其中包含给定分类法的子术语的term_id、名称、段塞等。我已经将这个数组保存到$tax_terms中。我有一个不同的数组--$active_filters-containing只是与某些分类法术语相匹配的词组;例如,来自var_dump of $active_filters:
array (size=3)
0 => string 'term-1' (length=6)
1 => string 'term-2' (length=6)
2 => string 'term-3' (length=6)我想知道是否有一种方法可以运行一个foreach,将这个术语段塞数组与get_terms返回的数组中的字符串进行比较,并为任何匹配的段塞返回name?因此,上面的例子将输出“术语1”、“术语2”、“术语3”。到目前为止,我尝试过使用array_search,但没有结果。任何援助都是非常感谢的。
发布于 2018-07-17 02:29:43
也许是数组_地图?
这将为您提供与$active_filters中的鼻涕虫匹配的一系列术语名称:
$matched_terms = array_map(function($term) use ($active_filters){
if(in_array($term->slug, $active_filters)){
return $term->name;
}
else{
return false;
}
}, $tax_terms);
//remove the empty array elements which came from terms which didn't match a slug
$matched_terms = array_filter($matched_terms);https://wordpress.stackexchange.com/questions/308755
复制相似问题