从WP Term对象获取术语名称

时间:2018-01-24 作者:MysticUnicorn

我使用以下表达式创建了一个数组:

 $taxonomies = get_terms(array(
            \'taxonomy\' => \'product_cat\',
            \'hide_empty\' => false,
 ));
我得到了这个作为回报:

Array ( 
[0] => WP_Term Object ( [term_id] => 79 [name] => Édességek [slug] => edessegek [term_group] => 0 [term_taxonomy_id] => 79 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 1 [filter] => raw [meta_value] => 0 ) 
[1] => WP_Term Object ( [term_id] => 55 [name] => Ételek [slug] => etelek [term_group] => 0 [term_taxonomy_id] => 55 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 0 [filter] => raw [meta_value] => 0 ) 
[2] => WP_Term Object ( [term_id] => 76 [name] => Glutén mentes ételek [slug] => gluten-mentes-etelek [term_group] => 0 [term_taxonomy_id] => 76 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 1 [filter] => raw [meta_value] => 0 ) 
[3] => WP_Term Object ( [term_id] => 81 [name] => Heti ajánlat [slug] => heti-ajanlat [term_group] => 0 [term_taxonomy_id] => 81 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 0 [filter] => raw [meta_value] => 0 ) 
[4] => WP_Term Object ( [term_id] => 49 [name] => Indiai ételek [slug] => indiai-etelek [term_group] => 0 [term_taxonomy_id] => 49 [taxonomy] => product_cat [description] => [parent] => 55 [count] => 0 [filter] => raw [meta_value] => 0 ) 
[5] => WP_Term Object ( [term_id] => 73 [name] => Kedvenc ételek [slug] => kedvenc-etelek [term_group] => 0 [term_taxonomy_id] => 73 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 1 [filter] => raw [meta_value] => 0 ) 
[6] => WP_Term Object ( [term_id] => 48 [name] => Krémlevesek [slug] => kremlevesek [term_group] => 0 [term_taxonomy_id] => 48 [taxonomy] => product_cat [description] => [parent] => 55 [count] => 1 [filter] => raw [meta_value] => 0 ) 
[7] => WP_Term Object ( [term_id] => 47 [name] => Levesek [slug] => levesek [term_group] => 0 [term_taxonomy_id] => 47 [taxonomy] => product_cat [description] => [parent] => 55 [count] => 0 [filter] => raw [meta_value] => 0 ) 
[8] => WP_Term Object ( [term_id] => 61 [name] => ph levesek [slug] => ph-levesek [term_group] => 0 [term_taxonomy_id] => 61 [taxonomy] => product_cat [description] => [parent] => 47 [count] => 0 [filter] => raw [meta_value] => 0 ) 
[9] => WP_Term Object ( [term_id] => 78 [name] => Saláták [slug] => salatak [term_group] => 0 [term_taxonomy_id] => 78 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 1 [filter] => raw [meta_value] => 0 ) 
[10] => WP_Term Object ( [term_id] => 77 [name] => Szendvicsek [slug] => szendvicsek [term_group] => 0 [term_taxonomy_id] => 77 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 1 [filter] => raw [meta_value] => 0 ) 
[11] => WP_Term Object ( [term_id] => 75 [name] => Tejmentes ételek [slug] => tejmentes-etelek [term_group] => 0 [term_taxonomy_id] => 75 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 1 [filter] => raw [meta_value] => 0 ) 
[12] => WP_Term Object ( [term_id] => 60 [name] => Új hűsítő levesek [slug] => uj-husito-levesek [term_group] => 0 [term_taxonomy_id] => 60 [taxonomy] => product_cat [description] => [parent] => 47 [count] => 0 [filter] => raw [meta_value] => 0 ) 
[13] => WP_Term Object ( [term_id] => 59 [name] => Új levesek [slug] => uj-levesek [term_group] => 0 [term_taxonomy_id] => 59 [taxonomy] => product_cat [description] => [parent] => 47 [count] => 0 [filter] => raw [meta_value] => 0 ) 
[14] => WP_Term Object ( [term_id] => 74 [name] => Vegaséf max ételek [slug] => vegasef-max-etelek [term_group] => 0 [term_taxonomy_id] => 74 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 1 [filter] => raw [meta_value] => 0 ) )
我只需要[name] 以及[slug] 在2个独立阵列中。怎么可能呢?我在想array_columnarray_push 选项

2 个回复
最合适的回答,由SO网友:birgire 整理而成

这里有一个使用handywp_list_pluck():

$terms = get_terms(array(
        \'taxonomy\' => \'category\',
        \'hide_empty\' => false,
));
$slugs = wp_list_pluck( $terms, \'slug\' ); 
$names = wp_list_pluck( $terms, \'name\' );
在这里,我们将通缉区拉入一个阵列。

SO网友:janh

听起来很奇怪,但我看到了一些奇怪的要求,所以我相信你有很好的理由。

$names = array();
$slugs = array();
$terms = get_terms(array(
    \'taxonomy\' => \'product_cat\',
    \'hide_empty\' => false,
));
foreach($terms as $term) {
    array_push($names, $term->name);
    array_push($slugs, $term->slug);
}
将创建这两个数组。注意,我已经重命名了您调用的变量$taxonomies 因为当它包含另一个事物时,用一个事物来命名它是令人困惑的(在本例中,它将包含术语,而不是分类法)。

结束