Risposte nei forum create
-
AutorePost
-
FraniusPartecipante
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.
FraniusPartecipanteA 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
FraniusPartecipanteScusa 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?
FraniusPartecipanteDovrei 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…
FraniusPartecipanteIn 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…
FraniusPartecipante$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″
FraniusPartecipanteOk, 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…
FraniusPartecipanteCiao 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…
FraniusPartecipanteGrazie!
E’ il quarto sito che porto alla 3.1, ma è la prima volta che mi succede…
FraniusPartecipanteSuccedeva anche a me.
Alla fine ho cambiato plugin, ora uso BackWPup
-
AutorePost