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!
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!
Share Improve this question edited Mar 2, 2015 at 0:28 Marco asked Mar 1, 2015 at 20:57 MarcoMarco 2091 gold badge5 silver badges13 bronze badges 2- So do you want all images for a single post in a custom post type, or all images for all posts of a custom post type? – Jason Murray Commented Mar 1, 2015 at 20:59
- All the image urls from a post. The feed is outputting all the posts from a custom post type. – Marco Commented Mar 1, 2015 at 21:15
2 Answers
Reset to default 5You 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.
use this code in the post loop.
$attimages = get_attached_media('image', $post->ID);
foreach ($attimages as $image) {
echo wp_get_attachment_url($image->ID).'<br>';
}
this code will return all attached images url