natnai
natnai

Reputation: 564

WordPress: Display Post Images on Front Page

Hoping someone can help. I've put this code into a standard template but no images are displaying, despite the fact that I've got a bunch of posts with images.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

$images =& get_children( array (
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));

if ( empty($images) ) {
    // no attachments here
} else {
    foreach ( $images as $attachment_id => $attachment ) {
        echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
    }
}

endwhile; endif; ?>

Thanks for helping!

Upvotes: 0

Views: 186

Answers (2)

Pieter Goosen
Pieter Goosen

Reputation: 9951

You can do something like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo wp_get_attachment_image( $attachment->ID, 'full' );
          }
     }

 endwhile; endif; ?>

This code will fetch all the 'large' images from the media library and display them

Hope this is what you were looking for

Upvotes: 0

Dinesh
Dinesh

Reputation: 4110

change

echo wp_get_attachment_image( $attachment_id, 'thumbnail' );

to

echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );

and

$images =& get_children( array (
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));

to

$images =get_posts( array (
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));

you should use get_posts()

Upvotes: 1

Related Questions