1

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!

  • 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 Mar 1 '15 at 20:59
  • All the image urls from a post. The feed is outputting all the posts from a custom post type. – Marco Mar 1 '15 at 21:15
1

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.

  • Will test it now, is the last tag open? and } necessary? – Marco Mar 1 '15 at 21:48
  • @Marco assuming some kind of php code immediately follows this piece of code the last tag should be open yes, if you are echoing out more tags you can just stick a ?> on the end. So it will be either as posted <?php } or <?php } ?> – Jason Murray Mar 1 '15 at 21:51
  • Gotcha, though I added the above if( statement in function.php and it crashed my site. I should of mentioned that I was using a commercial theme which is heavily customized. – Marco Mar 1 '15 at 22:00
  • @Marco can you update the question with the whole section of functions.php relating to this and any front end code. – Jason Murray Mar 1 '15 at 22:04
  • I updated my question with the XML file that I'm using. – Marco Mar 1 '15 at 22:24
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.