Home › Forum › Problemi Vari con WP › get_currentuserinfo(); non funziona in Header.php
-
AutorePost
-
-
6 Settembre 2010 alle 15:10 #14963ManuelHTPartecipante
Ciao a tutti, ho fatto un piccolo script che prende il nome dell’utente loggato e mostra una lista degli ultimi articoli che ha commentato. L’ho inserito nel file index.php del tema e funziona ma siccome mi serva che stia in alto l’ho inserito nell’header.php subito dopo l’apertura del body manon funziona.
E’ come se riesce a vedere che l’utente è loggato e vede anche l’ID utente ma non mi trova gli altri dati come il nome di login, il nome utente o altre cose.
Da cosa dipende? Grazie
-
6 Settembre 2010 alle 18:15 #76936reallocPartecipante
Hai letto tutta la pagina su questa funziona?
http://codex.wordpress.org/Function_Reference/get_currentuserinfo
-
7 Settembre 2010 alle 11:57 #76948ManuelHTPartecipante
Si perchè? Cosa mi è sfuggito?
-
7 Settembre 2010 alle 12:14 #76949reallocPartecipante
Siccome non hai fatto vedere il codice che usi posso solo ipotizzare. Nella pagina si trovano due esempi sotto “Using Separate Globals”. Poi l’utlimo esempio è un buon punto di partenza e ti dà la possibilità di usare magari questa funzione -> http://codex.wordpress.org/Function_Reference/get_userdata …
-
7 Settembre 2010 alle 12:22 #76951ManuelHTPartecipante
Si non ho messo il codice ma ho detto che nella index funziona mentre nell’header no quindi non capisco quale sia la differenza tra metterlo nella index e nell’header.
Comunque ecco il codice:
global $current_user;
get_currentuserinfo();
$sql="SELECT * FROM wp_comments WHERE user_id='$user_ID' GROUP BY comment_post_ID ORDER BY comment_ID DESC" ;
$query=@mysql_query($sql) or die (mysql_error());
$array=mysql_fetch_array($query);
while ($array=mysql_fetch_array($query))
{
$post_ID= $array;
echo $post_ID.", ";
$sql_post="SELECT post_title FROM wp_posts WHERE ID='$post_ID'" ;
$query_post=@mysql_query($sql_post) or die (mysql_error());
$array_post=mysql_fetch_array($query_post);
$post_titolo= $array_post;
echo $post_titolo.', ';
}
Poi ho un altro problema, in pratica uso GROUP BY per raggruppare gli ID dei post e li ordino in base al ID del commento solo che non me li fa vedere né in base al ID dei commenti né in base al ID dei post
-
7 Settembre 2010 alle 12:51 #76953reallocPartecipante
Allora metti il codice come funzione nel file functions.php. Dopo la puoi usare dove vuoi.
Per il secondo problema basta uno statement:
SELECT DISTINCT(c.comment_post_ID) AS post_id, p.post_title FROM wp_comments c, wp_posts p WHERE c.comment_post_ID = p.ID AND c.user_id=$user_ID ORDER BY c.comment_ID DESC
Guarda anche la WPDB-Class
-
7 Settembre 2010 alle 20:05 #76960ManuelHTPartecipante
Eppure non so perchè ma ancora mi mostra i commenti sballati.
Mi mostra per primo il commento al post 87 che è il penultimo che ho commentato. Poi mi mostra il 423 che è stato commentato molto tempo fa, poi il 783, 773, 746 ecc e poi tutti in ordine decrescente però di ID post e non di commento.
Come mai?
-
7 Settembre 2010 alle 20:22 #76961SteveAglAmministratore del forum
Bhe se scrivi:
global $current_user;
get_currentuserinfo();
$sql=”SELECT * FROM wp_comments WHERE user_id=’$user_ID’ GROUP BY comment_post_ID ORDER BY comment_ID DESC” ;
Dubito matchi l’utente loggato il cui ID si trova (come indicato negli esempi del codex in $current_user->ID mentre tu fai user_id=’$user_ID’ quindi a mano che tu abbai del codice che non ci hai mostrato cosi’ non andrà mai… o metti user_id=’$current_user->ID’ o metti anche global $user_ID assieme all’altra global…
-
7 Settembre 2010 alle 20:43 #76962ManuelHTPartecipante
Il codice completo messo nel functions.php è questo:
function post_comments() {
global $current_user;
get_currentuserinfo();
global $user_ID;
if ($user_ID == '1') {
$sql="SELECT DISTINCT (c.comment_post_ID) AS post_id, p.post_title FROM wp_comments c, wp_posts p WHERE c.comment_post_ID = p.ID AND c.user_id=$user_ID ORDER BY c.comment_ID DESC LIMIT 6" ;
$query=@mysql_query($sql) or die (mysql_error());
$array=mysql_fetch_array($query);
while ($array=mysql_fetch_array($query))
{
$post_ID= $array;
echo $post_ID.", ";
$post_titolo= $array;
echo $post_titolo.', ';
}
}
}
Come vedi le variabili globali le ho richiamate.
PS: L'istruzione if l'ho messa solo per fare delle prove in modo che i risultati siano visibili solo se ci si logga come amministratore e quindi user_ID=1.
-
7 Settembre 2010 alle 21:17 #76963SteveAglAmministratore del forum
e a me quella funzione in fucntion.php e poi richiamata in sidebar funziona e mostra l’utente admin (se loggato come tale), quindi la get_currentuserinfo(); torna quello che deve… se non lo fa hai altre cose che interferiscono…
-
7 Settembre 2010 alle 21:36 #76964ManuelHTPartecipante
Si ma il problema ora non è che non mi fa vedere gli utenti, e che mi fa vedere i commenti non in base agli ultimi inseriti dall’utente.
E’ molto strano perché comunque la query è molto precisa e non lascia molto spazio ad errori secondo me. Cosa potrebbe mai interferire con quello?
-
8 Settembre 2010 alle 8:08 #76965reallocPartecipante
DISTINCT e GROUP BY fanno cose simili … questo statement è ancora più preciso
SELECT c.comment_post_ID, MAX(c.comment_ID) AS comment_ID, p.post_title FROM wp_comments c, wp_posts p WHERE c.comment_post_ID = p.ID AND c.user_id=$user_ID GROUP BY c.comment_post_ID ORDER BY c.comment_ID DESC
-
8 Settembre 2010 alle 12:16 #76972ManuelHTPartecipante
Ho risolto il problema con la tua query però togliendo
$array=mysql_fetch_array($query);
prima del while e mettendo "ORDER BY comment_ID" invece che "ORDER BY c.comment_ID".
Ora funziona bene grazie a tutti e due
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.