Home › Forum › Amministrazione WP › Visualizzare il vaolre di una meta_key
-
AutorePost
-
-
17 Giugno 2011 alle 16:05 #18232FraniusPartecipante
Salve,
vorrei visualizzare all’interno di un articolo il valore di una meta_key della’rticolo stesso.
Mi spiego, tra le meta_key dell’articolo Explorer1 c’e nazione=U.S.A.
Come faccio a visualizzare il valore (in questo caso U.S.A. ma in altri articoli puo’ cambaire) della meta_key=nazione?
Ho rpovato a impostare una cosa del tipo
<?php
$nazione = get_post_meta($post->ID, 'nazione', false);
echo "$nazione" ;
?>Ma non ottengo nessun risultato…
Dove sbaglio?
-
17 Giugno 2011 alle 17:25 #85591SteveAglAmministratore del forum
Leggendo:
http://codex.wordpress.org/Function_Reference/get_post_meta
vedrai che il terzo paraametro se messo a false (il valore predefinito) torna un array e tu stampi una stringa… mettici un true se sei certo che sia univoco quel meta key per un articolo et voilà deve andare
-
20 Giugno 2011 alle 15:23 #85688FraniusPartecipante
Ciao Steve,
ho fatto un po’ di test…
Non c’è verso di far stampare a video il valore di una meta_key all’interno di un articolo (contenent o excerpt).
Viceversa, se faccio una query per tirare fuori N articoli, e per ogni risultato gli dico di visualizzare anche il valore di una o piu’ meta_key, allora il valore viene visualizzato.
Ad es. cosi’:
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#b0b0ce">
<tbody>
<?php
global $post;
$rowcnt = 0;
$rowitems = 4; // Numero di celle per riga
$width = (int) (100 / $rowitems);
$args = array(
'numberposts' => -1, // Numero di post per pagina, -1 = infiniti
'post_status' => null,
'category_name' => 'space-mission',
'orderby' => 'date',
'order' => 'ASC',
);
$myposts = get_posts($args);
foreach($myposts as $post) :
$titolo = get_the_title($post->ID);
$nazione = get_post_meta($post->ID, 'nation', true);
$lancio = get_post_meta($post->ID, 'launch', true);
$note = get_post_meta($post->ID, 'Note', true);
if (++$rowcnt == 1) {
echo "<tr valign='top' align='center'>";
}
echo "<td width='$width%'><p><a href='";
the_permalink();
echo "'>$post->post_excerpt $titolo</p> <b>NAZIONALITA':</b> $nazione <b>DATA LANCIO:</b> $lancio <b>NOTE:</b> $note</td>";
if ($rowcnt == $rowitems) {
$rowcnt = 0;
echo "</tr>";
}
endforeach;
if ($rowcnt == 0) {
echo "</tr>";
}
?>
</tbody>
</table>
<?php
wp_reset_query();
?>
Con la query di sopra, gli dico di stampare per ogni risultato: il permalink, l’excerpt e il tiotolo in modo che siano un link verso l’articolo vero e proprio e il valore di 3 metakey come informazioni aggiuntive.
Ma, ripeto, se faccio le stesse chiamate dall’interno di un singolo articolo non ottengo risultati…
-
20 Giugno 2011 alle 16:05 #85692SteveAglAmministratore del forum
facciamo che dopo questo:
$titolo = get_the_title($post->ID);
$nazione = get_post_meta($post->ID, ‘nation’, true);
$lancio = get_post_meta($post->ID, ‘launch’, true);
$note = get_post_meta($post->ID, ‘Note’, true);
fai un var_dump delle variabili e vediamo che cosa scrive?
Le basi del debug
-
21 Giugno 2011 alle 9:52 #85707FraniusPartecipante
Ok, Steve…
Qui ci addentriamo in un campo che va al di là delle mie conoscenze.
Non sono un programmatore, tutto quello che so di PHP l’ho imaparato “per colpa” di WP…
Quindi, grazie infinite per il supporto.
Allora…
Se faccio il var_dump ottengo bool(false)
ho provato anche con var_export e ottengo false
Con print_r invece non ottengo nulla…
-
21 Giugno 2011 alle 10:31 #85708SteveAglAmministratore del forum
cioè il vardump di tutte e 4 le variabili da false? Pure $titolo?
-
21 Giugno 2011 alle 11:14 #85709FraniusPartecipante
$titolo lo usavo solo nella pagina della query (questa)
Una volta messo nell’articolo ottengo: string(9) “Sputnik 1″ (che va piu’ o meno bene, ovviamente non mi serve sapere che il contenuto è una stringa di 9 caratteri)
L’articolo dove sto facendo le prove è questo e qui sotto c’è il codice che sto usando
<?php
$nazione = get_post_meta($post->ID, 'nation', true);
$lancio = get_post_meta($post->ID, 'launch', true);
$note = get_post_meta($post->ID, 'Note', true);
$titolo = get_the_title($post->ID);
?>
<?php
var_dump($nazione);
var_dump($lancio);
var_dump($note);
var_dump($titolo);
?>
Il risultato è:
bool(false)
bool(false)
bool(false)
string(9) “Sputnik 1″
-
21 Giugno 2011 alle 12:14 #85710SteveAglAmministratore del forum
Siamo sicuri dei nomi dei campi vero? é nation? perchè pare torni nulla come se l’articolo in questione non abbia valori per quella metakey
-
21 Giugno 2011 alle 12:58 #85712FraniusPartecipante
In questa pagina si vedono visualizzati i 3 meta_value delle meta_key per ogni articolo.
Lo Sputnik 1 è il primo in alto a sinistra, nella tabella generata con la query (vedi piu’ su, secondo reply dopo l’apertura della discussione) i tre valori vengono visualizzati.
Ma se clicki per andare all’articolo (qui), si ottiene quello di cui stiamo parlando (ovvero nessun risultato).
Ripeto, non sono un programmatore, ma non riesco proprio a capire perchè un semplice echo $nomevaribile funziona se genero un elenco di articoli, ma non funziona dentro l’articolo…
-
21 Giugno 2011 alle 13:28 #85714SteveAglAmministratore del forum
cambia
foreach($myposts as $post)
in
foreach($myposts as $mypost)
e ovviamente i $post->ID in $mypost->ID
visto che $post la globalizzi e quella è uan variabile gestita da WP
-
21 Giugno 2011 alle 13:46 #85716FraniusPartecipante
Dovrei cambiare la query nella pagina?
Ok fatto, ma non vedo a cosa posssa servire (pagina di test).
L’arcano è che non ottengo la stampa dei valori nel singolo articolo, valori invece visualizzati nella pagina contenente la query che genera la lista di articoli…
-
21 Giugno 2011 alle 14:14 #85720SteveAglAmministratore del forum
ho detto di cambiare la query della pagina? NO ho detto di cambiare uno specifico foreach e i conseguenti riferimento a $post (dopo il forech).
Direi che è al di la delle tue possibilità sta cosa, per debuggarlo io guarderei il db etc etc… ma qua non ne veniamo a capo più.. sugggerisco un professionista che risolva la cosa (eventualemnte scrivi)
Altrimenti devi accontentarti diq uello che ottieni con WP e plugin, mettersi a paciugare codice mettendo assieme pezzi presi in giro è come voler fare la manutenzione della propia auto andando per sfasciacarrozze e ricambisti auto sapendo al massimo guaidarla e farci benzina.
-
21 Giugno 2011 alle 14:51 #85725FraniusPartecipante
Scusa Steve,
la mia richiesta era partita da un banale: come mai se faccio “get_post_meta” all’interno di un post di WP non vi visualizza il valore della meta_key indicata?
Ho letto nella pagina codex di WP la sintassi del comando, ma visto che non mi sembra ci siano errori in quello che scrivo, qualcuno puo’ aiutarmi?
Non ho mai preteso di mettermi a guardare il db o cose simili.
Non è il primo sito che faccio con WP, e ormai un po’ di dimestichezza negli anni credo di averla acquisita (ad es. qui c’è una pagina con diverse query dove incrocio una actegory, 2 tag ed escudendo un arrayy di 3 tag).
E mi sono sempre documenatto sulle pagine del codex.
Tu mi dici di cambaire uno specifico foreach, ma non nella pagina.
Peccato che nell’articolo non c’è nessun foreach, ma c’e’ soltanto nella pagina.
Cosa dovevo capire?
-
21 Giugno 2011 alle 15:24 #85730FraniusPartecipante
A quanto pare non sono l’unico ad aver avuto questo problema (segno quindi che ll comando cosi’ com’è NON funziona).
vedi forum inglese.
usando il comando get_the_id() ivece di $post->ID finalmente si ottiene qualcosa.
vedi mio articolo modificato
-
21 Giugno 2011 alle 16:35 #85733SteveAglAmministratore del forum
Nel codice che hai posto in origine leggo:
$myposts = get_posts($args);
foreach($myposts as $post) :
$titolo = get_the_title($post->ID);
$nazione = get_post_meta($post->ID, ‘nation’, true);
$lancio = get_post_meta($post->ID, ‘launch’, true);
$note = get_post_meta($post->ID, ‘Note’, true);
quello NON è un foreach, che poi il comando abbia in certi caisiun bug non documentato nemmeno io lo sapevo… per quellocheidevo di satampare il post->ID diquel foreach per capire che cosa ci fosse dentro… ora non riesco a guardare il trac per vedere se esite un ticket a riguardo e nel caso aprirlo…
-
21 Giugno 2011 alle 20:37 #85754FraniusPartecipante
Steve,
nessuno pensa che ci sia qualcuno che conosca tutti i bug di WP, ci mancherebbe altro.
Evidentemente non mi sono spiegato bene…
Quello che io ho chiesto all’inizio è stato:
<?php
$nazione = get_post_meta($post->ID, 'nazione', false);
echo "$nazione" ;
?>
Come mai get_post_meta non da’ risultati?
Per provarti che meta_key e meta_value fossero esistenti e scritti in maniera corretta, ti ho fatto l’esempio della query (dove c’è il foreach che mi hai detto di cambiare).
Resta il fatto che il semplice codice qui sopra (e con cui ho aperto il topic) non funziona.
Per ottenere dei risultati bisogna usare
$nomevariabile = get_post_meta(get_the_id(), 'meta_key', false);
Bisogna quindi usare get_the_id() invece di $post->ID .Non so se è un bug o se è un errore della pagina del codex, fatto sta che seguendo le istruzioni del codex non funziona.
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.