0
votes

I am trying to show images that are attached to a page/post in Wordpress.

I don't want to insert anything into the post only attach it.

Just now I have clicked 'Add Media' and chosen multiple images that were already uploaded in the past and have used the following PHP.

$images =& get_children( array (
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));
print_r($images);
if ( empty($images) ) {
    // no attachments here
} else {
    foreach ( $images as $attachment_id => $attachment ) {
        echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
    }
}

This doesn't pick up attached images but if I upload an image to this post and then attach it as above then it works.

So my question is, how do I get previously uploaded to the site content to show in my code above

2
'Add Media' of existing images does not attach the inserted image to the post. The image remains attached to the original post it was uploaded to. Therefore it is not an attachment of the post it was inserted into. If the existing image is not attached you can use Media Library to attach it to a post.user2080039
'Add Media' merely inserts HTML into your content which should already display the image. So you don't need to code any PHP - I don't quite understand why you want the PHP.user2080039

2 Answers

0
votes

WordPress associates an image with the post where the image is uploaded and that's a attachment.

With your function, you'll get only the images you uploaded to that post, as it's happening now.

In if you want to associate a group of images to a post the most straightforward way to do it is creating a gallery in that post, and then control the gallery output using post_gallery filter.

0
votes

I was able to overcome the issue with a plugin called Attachments view plugin