Come ottenere il nome di un termine tassonomico dal suo slug?

5 mag 2011, 07:47:46
Visualizzazioni: 147K
Voti: 42

Se conosco lo slug di un termine tassonomico, come posso ottenere il nome di quel termine?

1
Commenti

vuoi creare un link, un titolo, ???

xLRDxREVENGEx xLRDxREVENGEx
5 mag 2011 08:10:50
Tutte le risposte alla domanda 3
4
67

La funzione che stai cercando è get_term_by. Puoi utilizzarla in questo modo:

<?php $term = get_term_by('slug', 'my-term-slug', 'category'); $name = $term->name; ?>

Questo restituirà in $term un oggetto contenente le seguenti proprietà:

term_id
name
slug
term_group
term_taxonomy_id
taxonomy
description
parent
count

La documentazione ufficiale spiega molto bene questa funzione: https://developer.wordpress.org/reference/functions/get_term_by/

5 mag 2011 08:11:15
Commenti

mi hai preceduto. È esattamente quello che farei anch'io.

xLRDxREVENGEx xLRDxREVENGEx
5 mag 2011 08:16:04

E se non hai lo slug della tassonomia?

EkoJR EkoJR
7 mag 2017 04:42:44

Puoi usare get_term( $term_id ); se hai solo l'ID.

Gavin Gavin
11 lug 2020 12:32:24

Sii consapevole che questo non è il modo per ottenere il nome della tassonomia, questo è il modo per ottenere il nome del termine.

jave.web jave.web
2 mag 2021 21:00:30
0

Questo fornisce una risposta quando la tassonomia non è disponibile/sconosciuta.

Nel mio caso, quando utilizzavo get_term_by, c'erano alcune istanze in cui era disponibile solo lo Slug del Termine (senza ID del Termine o Tassonomia). Il che mi ha portato qui. Tuttavia, la risposta fornita non ha risolto completamente il mio problema.

Soluzione per $taxonomy vuoto

// Vogliamo trovare l'ID corrispondente a questo slug.
$term_slug = 'foo-bar';
$taxonomies = get_taxonomies();
foreach ( $taxonomies as $tax_type_key => $taxonomy ) {
    // Se viene restituito un oggetto termine, interrompi il ciclo. (Restituisce false se non c'è alcun oggetto)
    if ( $term_object = get_term_by( 'slug', $term_slug , $taxonomy ) ) {
        break;
    }
}
$term_id = $term_object->name;

echo 'L\'ID del Termine è: ' . $term_id . '<br>';
var_dump( $term_object );

Risultato

L'ID del Termine è: 32
object(WP_Term)
  public 'term_id' => int 32
  public 'name' => string 'Termine di Esempio'
  public 'slug' => string 'termine-esempio'
  public 'term_group' => int 0
  public 'term_taxonomy_id' => int 123
  public 'taxonomy' => string 'category'
  public 'description' => string ''
  public 'parent' => int 0
  public 'count' => int 23
  public 'filter' => string 'raw'

Come segue, il concetto ottiene un array di $taxonomies, scorre l'array e SE get_term_by() restituisce una corrispondenza, interrompe immediatamente il ciclo foreach.

Nota: Ho provato a cercare un metodo per ottenere la tassonomia associata (ID o Slug) dallo Slug del Termine, ma purtroppo non sono riuscito a trovare nulla disponibile in WordPress.

7 mag 2017 06:53:29
1

grazie, questa soluzione ha funzionato per me.

Ho creato una funzione e la riutilizzo tutte le volte che mi serve.

function helper_get_taxonomy__by_slug($term_slug){
    $term_object = "";
    $taxonomies = get_taxonomies();
    foreach ($taxonomies as $tax_type_key => $taxonomy) {
        // Se viene restituito l'oggetto term, interrompi il loop. (Restituisce false se non c'è un oggetto)
        if ($term_object = get_term_by('slug', $term_slug, $taxonomy)) {
            break;
        }else{
            $term_object = "Attenzione! Tassonomia helper non trovata.";
        }
    }
    return $term_object;
}
3 gen 2019 17:09:55
Commenti

Dovresti restituire gli stessi tipi di get_term_by: (WP_Term|array|false) Istanza WP_Term (o array) in caso di successo. Restituirà false se $taxonomy non esiste o $term non viene trovato.

xnagyg xnagyg
25 mag 2020 20:58:26