Wordpress: visualizzare un'immagine a caso

posted by liliansi on 2009-06-25 00:00:00
Ciao a tutti. Approfitto subito di questo servizio che ho scoperto oggi e comincio a snocciolare le mie questioni.
Ho creato una gallery di immagini prese a caso dai post del mio blog usando questo comando:
<?php $hilite = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY RAND() LIMIT 1" ); echo '<a href="' . get_permalink($hilite) . '" title="'.wptexturize($hilite->post_title).'">' . wp_get_attachment_image( $hilite, 'thumbnail' ) . '</a>'; ?>
Potete vedere la gallery nel footer del mio blog http://www.dren.it
Funziona benissimo e l'idea è molto carina. L'unico problema è che il link dell'immagine mi riporta solo all'immagine mentre io vorrei riportasse al post da cui è tratta e ugualmente vorrei che l'alt dell'immagine equivalesse al titolo del post da cui è tratto.
Lo so, sono esosa, però se si potesse fare una cosa del genere sarebbe davvero una funzione utile!
Ringrazio in anticipo tutti quelli che mi daranno una mano in tal proposito e faccio i complimenti e gli auguri ai creatori del sito.
camu
3
Allora, mi sembra abbastanza semplice da mettere in pratica. Partiamo con la prima richiesta: estrapolare il permalink del post a cui quell'immagine è associata. Oltre a selezionare l'ID, ti serve il campo "post_parent" di quella tabella. Visto che vuoi anche il titolo del post, selezionerai anche quello, in una sola query:
 
SELECT t1.ID, t2.ID as postID, t2.post_title
FROM $wpdb->posts t1, $wpdb->posts t2
WHERE t1.post_type = 'attachment' and t1.post_parent = t2.ID
ORDER BY RAND() LIMIT 1
 
Lo so, si potrebbe fare una left join, ma let's keep it simple. Questa stringa ti restituisce una riga della tabella con tre campi. Usiamoli
 
$myData = $wpdb->get_row(" select di cui sopra ");
echo '<a href="' . get_permalink($myData->postID) . ' " alt="'.wptexturize($myData->post_title).'">' . wp_get_attachment_image( $myData->ID, 'thumbnail' ) . '</a>';
 
Non ho provato, ma ad occhio dovrebbe funzionare :)
2 answers - 0 questions
+ 2  Positive        Negative

liliansi avatar
liliansi
0
Ohhh, bravissimo grazie, ora ogni immagine è linkata al post da cui è tratta. Rimane solo il problema dell'attributo alt ...che poi non è alt ma title perchè il mio scopo è quello di mostrare il titolo del post da cui è tratta l'immagine quando gli utenti si soffermano con il mouse sopra l'immagine. E' un'inezia lo so però perfezionerebbe questo codice che, a mio parere, risulterebbe utile a molti. Io ne capisco pochissimo di PHP, il poco che so l'ho imparato perchè progetto template per Wordpress. Ho provato a pasticciare col codice, ho modificato il parametro in tutte le maniere a me conosciute ma quando passo sopra al thumbnail mi appare sempre il titolo dell'immagine originale...che la maggior parte delle volte è qualche nome assurdo che non c'entra nulla.
4 answers - 1 questions
  Positive        Negative

softweb avatar
softweb
2
Unfortunately wp_get_attachment_image function does not populate alt attribute.
You can try something like this:
 
$myData = $wpdb->get_row(" select di cui sopra ");
echo '<a href="' . get_permalink($myData->postID) . ' " title="'.wptexturize($myData->post_title).'">' . str_replace('alt=""', 'alt="'.wptexturize($myData->post_title).'"', wp_get_attachment_image( $myData->ID, 'thumbnail' )) . '</a>';
 
Please liliansi and camu write your questions/answers in english.
Bye
7 answers - 7 questions
  Positive        Negative

liliansi avatar
liliansi
0
Excuse me! I'll speak english now. The code don't work yet. I'm searching for solution, when I found it I'll post here. Thanks very much for your help, I review this excellent site in my blog
4 answers - 1 questions
  Positive        Negative

dail avatar
dail
12
Thanks for the compliments, we try to do our best.
So liliansi, do you get a php error?
Let me know
49 answers - 0 questions
  Positive        Negative

dail avatar
dail
12
Posted by liliansi:
Excuse me! I'll speak english now. The code don't work yet. I'm searching for solution, when I found it I'll post here. Thanks very much for your help, I review this excellent site in my blog

Can you post me the result of:
 
echo wp_get_attachment_image( $myData->ID, 'thumbnail');
 
49 answers - 0 questions
  Positive        Negative

liliansi avatar
liliansi
0
The thumbs are well displayed. This part of code is allright. Only the TITLE attribute don't work
4 answers - 1 questions
  Positive        Negative

dail avatar
dail
12
Do you want to show ALT attribute (on the image)? or, Do you want to show TITLE attribute (on the link)?
Please, post here the results of:
 
echo wp_get_attachment_image( $myData->ID, 'thumbnail');
 
and
 
echo wptexturize($myData->post_title);
 
49 answers - 0 questions
  Positive        Negative

liliansi avatar
liliansi
0
This code didn't work properly because the ALT attribute don't show the post tile but the images name.
Stefano, the owner of http://asteriscoweb.net/, created a brand new cose for show an image gallery linked to the post where the images are:
<ul class="random_thumb">
 
<?php 
$n = 4; //number of images to show
$args = array(
	'post_type' => 'attachment',
	'numberposts' => $n,
	'post_status' => null,
	'post_parent' => null, 
	'orderby' => 'rand',
	); 
 
$att = get_posts($args);
 
foreach($att as $data){
 
	$img = wp_get_attachment_image_src($data->ID, 'thumbnail');
	$img_url = $img[0];
	$img_w = $img[1];
	$img_h = $img[2];
	$img_alt = $data->post_title;
 
	$url = get_permalink($data->post_parent);
	$title = get_the_title($data->post_parent);
 
	echo "\t<li>";
	echo "<a href=\"$url\" title=\"$title\"><img src=\"$img_url\" alt=\"$img_alt\" width=\"$img_w\" height=\"$img_h\" /></a>";
	echo "</li>\n";
 
}
 
?>
</ul>
 
 
This code work properly now, the ALT is shown e you can choose the number of images.
Thanks a lot to everybody for your help. Bye!
4 answers - 1 questions
  Positive        Negative



Answer the question



Top Users
  • dail (12)
  • livin52 (3)
  • camu (3)
  • softweb (2)
  • Nadine (1)
  • Josware (1)
  • lfelipecr (1)
  • gregoriohc (1)
  • Mitu (1)
  • ryan714 (1)