web-dev-qa-db-fra.com

Obtenir les noms de terme de WP Objet de terme

J'ai créé un tableau avec ces expressions:

 $taxonomies = get_terms(array(
            'taxonomy' => 'product_cat',
            'hide_empty' => false,
 ));

Et j'ai eu ceci comme retour:

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 ) )

Je n'ai besoin que du [name] et du [slug] in 2 tableau séparé. Comment c'est possible? Je pense aux options array_column et array_Push

1
MysticUnicorn

Voici une alternative en utilisant le pratique wp_list_pluck() :

$terms = get_terms(array(
        'taxonomy' => 'category',
        'hide_empty' => false,
));
$slugs = wp_list_pluck( $terms, 'slug' ); 
$names = wp_list_pluck( $terms, 'name' );

où nous extrayons le champ voulu dans un tableau.

3
birgire

Cela peut sembler étrange, mais j'ai déjà pris connaissance d'exigences relatives aux étrangers. J'espère donc que vous avez une bonne raison à cela.

$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);
}

Va créer ces deux tableaux. Notez que j'ai renommé la variable que vous avez appelée $taxonomies car il est déroutant de la nommer après une chose lorsqu'elle en contient une autre (dans ce cas, elle contiendra des termes, pas des taxinomies).

1
janh