Support » Themes and Templates » displaying selected posts attachments on a page

  • I have a page which should:
    1) pull out specific posts (in the code below I’m just selecting them by category)
    2) pull out and display the attached images for those posts

    This is what I’ve got:

    $args = array(
    		'category_name'   => 'homepage',
    		'orderby'         => 'post_date',
    		'order'           => 'DESC',
    		'post_type'      => 'post'
    	$homepage_posts = get_posts( $args );
    	foreach( $homepage_posts as $the_post ) {
    		echo $the_post->ID;
    		$args = array(
    			'post_type' => 'attachment',
    			'numberposts' => -1,
    			'post_status' => null,
    			'post_parent' => $the_post->ID
    		$attachments = get_posts($args);
    		if ($attachments) {
    			foreach ( $attachments as $attachment ) {
    				echo apply_filters( 'the_title' , $attachment->post_title );
    				the_attachment_link( $attachment->ID , false );

    This is correctly echoing the post ID for each post fetched by the first get_posts(), but $attachments is coming out as an empty array.

    I’m confused about how ‘posts’ are ‘attached’ to other posts. In the blog admin, I’ve uploaded a number of images to my media library. I’ve then created some posts, and used the “Upload/Insert” icons above the post text editor to insert an image into each post, and published the posts.

    Are these images now considered to be attachments? Are they “posts” attached to their parent post? In my media library, these images are all listed as being attached to the “Home” page – currently the only page on the blog, and the one I’m trying to pull all these posts on to.

    How can I confirm that an image is “attached” to a post? More pertinently, why is the code above not showing the attachments for the selected posts, when the admin pages clearly show those images on the post-edit page?

    God, this is confusing 🙁

  • The topic ‘displaying selected posts attachments on a page’ is closed to new replies.