Obtener todas las imagenes del post
Para obtener todas las imagenes de un post o página WordPress podemos usar una sencilla función. Tener en cuenta que luego tendrán que aplicar el estilo necesario.
//Argumentos que usamos para obtener las imagenes
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
//argumentos que usaremos para el tamaño de las imagenes o para añadir más atributos por ejemplo si queremos crear una galería lightbox con prettyphoto
$default_attr = array(
'src' => $src,
'class' => "thumb",
'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )),
'title' => trim(strip_tags( $attachment->post_title )),
'rel' => 'prettyPhoto[mixed]',
);
$imagenes = get_posts($args);
if ($imagenes) {
echo '<ul>';
foreach ($imagenes as $imagen) {
echo '<li>';
echo wp_get_attachment_image( $imagen->ID, 'thumbnail', $default_attr);
echo '<p>';
echo apply_filters( 'the_title', $imagen->post_title );
echo '</p></li>';
}
echo '</ul>';
}
Y si lo que quieren es meter la imagen dentro de un link lo hacen así:
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
echo '<ul>';
foreach ($attachments as $attachment) {
$img_url = wp_get_attachment_image_src($attachment->ID, 'full');
echo '<li><a href="'.$img_url[0].'" rel="prettyPhoto[mixed]" class="thumb">';
echo wp_get_attachment_image( $attachment->ID, 'thumbnail');
echo '</a></li>';
}
echo '</ul>';
}
Espero que les sirva. Saludos!!
Acerca del autor
by Damián Logghe
Programador freelancer y emprendedor en sueños. Contento de ser mi jefe y poder hacer lo que me gusta. Wordpress es mi principal fuente de ingreso y me escribo tanto para ayudar como para tener una guia de memoria. Quieres contratarme? Déjame un mensaje.






Últimos Comentarios