WordPress.org

Forums

[resolved] Array all images attached to post (5 posts)

  1. chadvonlind
    Member
    Posted 4 years ago #

    I've put together this loop based on the Codex page for the get_children function. This works within a post's loop to spit out the URL of each attached image, and it spits them out perfectly.

    ie:
    http://domain.com/uploads/image01.jpg
    http://domain.com/uploads/image02.jpg
    http://domain.com/uploads/image03.jpg

    <?php // find images attached to this post / page.
    		$images =& get_children( 'post_parent='. $post->ID .'&orderby=menu_order&order=ASC&post_type=attachment&post_mime_type=image&numbersposts=-1' );
    		foreach ( (array) $images as $attachment_id => $attachment ) { ?>
                <?php echo wp_get_attachment_url( $attachment_id ); ?>
    <?php	}  ?>

    Where it gets weird on me, is when I try to use that URL within an image tag. I'm making use of timthumb, which is why I want the URL and not the normal image link

    When I change the code to what I have below, it will spit out all the images, but only 2 will include the src for the images. The rest will just have the original=[imageurl] included, but no src=[imageurl]. This is very odd to me.

    <?php // find images attached to this post / page.
    		$images =& get_children( 'post_parent='. $post->ID .'&orderby=menu_order&order=ASC&post_type=attachment&post_mime_type=image&numbersposts=-1' );
    		foreach ( (array) $images as $attachment_id => $attachment ) { ?>
               <img src="<?php bloginfo('template_url'); ?>/thumb.php?src=<?php echo wp_get_attachment_url( $attachment_id ); ?>&h=145&w=235&zc=1" alt="<?php the_title(); ?>" width="235" height="145" border="0" />
    <?php	}  ?>

    Any attachment loop experts out there have any idea as to why it wouldn't include the source for the rest of the images?

  2. chadvonlind
    Member
    Posted 4 years ago #

    I've found the solution, and it was even suggested on the get_children reference page in the Codex. Use get_posts instead! Here it is for anybody interested.

    <?php
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID );
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ( $attachments as $attachment ) { ?>
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_url'); ?>/thumb.php?src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>&h=145&w=235&zc=1" alt="<?php the_title(); ?>" width="230" height="140" border="1" /></a>
    
    <?php	}
    }
    ?>
  3. chadvonlind
    Member
    Posted 4 years ago #

    Hah. Crap. Now this code is doing the same thing as the previous code. What gives??

  4. chadvonlind
    Member
    Posted 4 years ago #

    I wonder if it has to do with some other tags. When I assign the attached images a order number, it seems to mess with things.

  5. chadvonlind
    Member
    Posted 4 years ago #

    **FACEPALM**

    It was the jQuery lazyload script interfering.

Topic Closed

This topic has been closed to new replies.

About this Topic