Come Ottenere l'ID della Tassonomia Personalizzata da un Post ID in WordPress

25 apr 2012, 08:54:18
Visualizzazioni: 16.8K
Voti: 1

Se conosco l'ID del post corrente che ho nella variabile $pid

Uso $terms = get_the_terms($pid, 'custom_category');

Come faccio a ottenere solo l'term id/term_taxonomy_id

Se faccio var_dump di $terms vedo quello che voglio...ma non ho idea di come estrarre solo l'id, non un array, solo l'id.

Tieni presente...che so meno della metà di quello che sto facendo...sto solo premendo tasti a caso per ottenere ciò che voglio...ma imparo ogni giorno di più.

0
Tutte le risposte alla domanda 2
5

(Probabilmente è meglio usare get_the_terms).

$terms =  wp_get_object_terms( $pid, 'custom_category', array('fields'=>'ids'));

Ottieni un array di ID dei termini (sarà sempre un array, anche se contiene un solo elemento):

$ids = wp_list_pluck( $terms, 'term_id' );

Se vuoi solo un ID... allora estrai l'ultimo ID:

$id = array_pop($ids);

Vedi anche la documentazione PHP su array_pop qui

25 apr 2012 09:55:44
Commenti

apprezzo la risposta... ma restituisce comunque un array da cui non so come estrarre l'id... sono proprio un principiante... non ho idea di cosa fare con il risultato.

Brian Thornton Brian Thornton
25 apr 2012 10:13:14

@BrianThornton ho modificato la mia risposta....

Rajeev Vyas Rajeev Vyas
25 apr 2012 10:15:56

ci siamo quasi... ma è tardi e faccio fatica a vedere... riprenderò domani... grazie per l'aiuto.

Brian Thornton Brian Thornton
25 apr 2012 11:23:59

@BrianThornton - avrai sempre un array perché i post possono avere più di un termine associato. Dovrai estrarre tu stesso l'ID. Userei get_the_terms invece di wp_get_object_terms.

Stephen Harris Stephen Harris
25 apr 2012 11:53:18

Quindi questo ha risposto alla mia domanda...le basi saranno la mia morte. Non riesco ancora a far funzionare ciò che voglio/ho bisogno...ma è complesso. Quindi sto scrivendo una domanda piuttosto lunga che pubblicherò tra poco. Grazie ragazzi...imparo ogni giorno di più qui!

Brian Thornton Brian Thornton
25 apr 2012 22:24:57
1
-1

Prova questo codice per ottenere l'ID della categoria dall'ID del post.

$categories = get_the_category( $post_id );
25 apr 2012 11:12:36
Commenti

Ad eccezione del fatto che, come riportato su http://codex.wordpress.org/Function_Reference/get_the_category, "Questa funzione restituisce solo risultati dalla tassonomia predefinita 'category'. Per tassonomie personalizzate, usa get_the_terms." Credimi...vorrei poterla usare...

Brian Thornton Brian Thornton
25 apr 2012 11:23:18