I’ve searched high and low and can’t seem to get it.
I’m trying to output an XML feed with all the images attached to a post from a custom post type:
</BasicDetails>
<Pictures>
<Picture>
<PictureUrl><?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID)); ?></PictureUrl>
<Caption></Caption>
</Picture><Picture>
<PictureUrl></PictureUrl>
<Caption></Caption>
</Picture>
</Pictures>
I’m using wp_get_attachment_url but it’s only returning one image (There’s more than one per post)
<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID)); ?>
The <Picture>
is a repeating element so it should start an new tree when there’s another image attached.
Any help would be Amazing!
You need to loop through the attachments within your post loop, replace the section of code you posted with this (put this together from some other code I found related to a similar problem, but couldn’t test it):
</BasicDetails>
<?php $args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => -1, // show all
'post_status' => 'any',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$images = get_posts($args);
if($images) { ?>
<Pictures>
<?php foreach($images as $image) { ?>
<Picture>
<PictureUrl><?php echo wp_get_attachment_url($image->ID); ?></PictureUrl>
<Caption><?php echo $image->post_excerpt; ?></Caption>
</Picture>
<?php } ?>
</Pictures>
<?php } ?>
<Agent>
EDIT – Updated based on asker edits.