Hoy veremos cómo usar miniaturas (thumbnails) para enlazar a nuestros artículos, que podrían ser utilizadas en nuestra barra lateral (sidebar.php) o, directamente, en nuestro índice (index.php). Para que esto funcione, cada artículo deberá tener una imagen subida al mismo (attachment) a través de la Librería de Imágenes (no sera necesario insertarlo en el cuerpo del artículo, con que el archivo esté subido basta. Esto nos puede ir bien si queremos distinguir entre imagen de presentación e imágenes finales en el texto principal una vez llegamos a la página concreta del artículo).

Tendremos que abrir el archivo en el que decidamos añadir esta funcionalidad y pegar el siguiente código:

<?php
$my_query = new WP_Query('showposts=12&orderby=rand');
if( $my_query->have_posts() ) {
echo '<h1>Artículos al Azar!</h1>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
$attachments = get_posts( array(
'post_type' => 'attachment',
'number_posts' => 1,
'post_status' => null,
'post_parent' => $my_query->post->ID,
) );
if ($attachments) {
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php
$thumbnail_id = $attachments[0]->ID;
echo wp_get_attachment_image( $thumbnail_id );
}
endwhile;
}
wp_reset_query();
?></a>
<?php endif; ?>

Estaremos aquí llamando a 12 artículos al azar.

Si tenemos problemas de maquetación, tendremos que usar CSS y abrir nuestro style.css para dar tamaños adecuados a esas imágenes:

.attachment-thumbnail {
height:50px;
width:50px;
padding:5px;
background:#fff;
margin:5px 5px 0 0;
float:left;
}

Esperamos vuestros comentarios.