[wp-hackers] Images attached to posts/pages?

Otto otto at ottodestruct.com
Mon Oct 5 19:23:48 UTC 2009


Attachments are essentially children of the post they're attached to.

They have the post_parent set to the id of the post, and they have a
post_type of "attachment".

To get attachments of a post:
$attachments = get_children( array('post_parent' => $post_id,
'post_type' => 'attachment') );

To only get image attachments:
$attachments = get_children( array('post_parent' => $id, 'post_type'
=> 'attachment', 'post_mime_type' => 'image') );


Once you have that, you can basically do this sort of thing:
foreach ( $attachments as $att_id => $attachment )
     echo wp_get_attachment_link($att_id, $size, true);

And so forth. There's several wp_get_attachment_* functions you can
use to get various bits from the attachment posts, once you have their
id's.

-Otto


On Mon, Oct 5, 2009 at 2:16 PM, Bryan Harley <bryanharley at gmail.com> wrote:
> Can someone explain the concept of how an image (when uploaded via the
> "Edit Post" or "Edit Page" pages) becomes attached to said post or
> page?
>
> If I were to go to the "About" page and upload 4 images from the image
> uploader and not insert any of these into my post body... those 4
> images are still associated will that page, correct?
>
> Is there any way to pull these images via PHP?  Is there a template or
> reference tag?  I can use the [gallery] shortcode, but what if I
> wanted the images displayed in a lightbox gallery for example?
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list