How do you get all the urls of images attached to a post?

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

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
Add a comment  | 

2 Answers 2

Reset to default 5

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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745042982a4607923.html

相关推荐

  • How do you get all the urls of images attached to a post?

    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

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信