How to query images from a post to use in a slider

I am trying to pull images as attachments from a particular post (custom post),

...and I'm using the example within WordPress codex codex example to display all images as a list

...except the images in that post keep repeating about 3 or 4 times (I have a total of 7 images attached to that post). no matter what I set the numberposts to or the post_per_page, I keep getting repeated images, about 28 when I should only have the seven.

If my numberposts is 1 or my post_per_image is 1 I only get 1 image of the 7 attached to that post... what am I doing wrong?

The code I'm using is below, and an example of what's happening on my site is here my test site where i'm trying to pull images from a post to use within FlexSlider

any help is very much appreciated. _Cindy

ul class="slides"   

    ?php if ( have_posts() ) : while ( have_posts() ) : the_post();    
        $args1 = array(
        'post_type' = 'attachment',
        'p = 107',
        'numberposts' = 7,
        'post_status' = 'inherit'
    );

$attachments = get_posts( $args1 );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {

    echo 'li';
    echo wp_get_attachment_image( $attachment-ID, 'home-slider' );
    echo '/li';
    }
}
endwhile; endif; ?

/ul

/div
/div
/div
/div !-- end 1080 pixels Container --

div class="grid-gradient-bg"/div
/div

!-- Slider Navigation --

div id="home-hero-nav" role=""
div class="container" !-- 1080 pixels Container --
div class="full-width columns"

    ul class="slider-menu thumbnails clearfix"

    ?php if ( have_posts() ) : while ( have_posts() ) : the_post();    
        $args2 = array(
        'post_type' = 'attachment',
        'p = 107',
        'numberposts' = 0,
        'post_status' = 'inherit'
    );

$attachments = get_posts( $args2 );

if ( $attachments ) {
foreach ( $attachments as $attachment ) {

    echo 'li';
    echo wp_get_attachment_image( $attachment-ID, 'home-slider-thumb' );
    echo '/li';
    }
}
endwhile; endif; ?


/ul

Topic wp-query Wordpress

Category Web


Here's the fix - taking the foreach loop out of the while loop. I also set attachments as get_children.

Hope this helps someone! Also see this great post on premiumdw!!

<?php    

$attachments = get_children(array('post_parent' => [post id], 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => [however many images attached to your post] ));

if ($attachments) { // if there are images attached to posting, start the flexslider markup

    foreach ( $attachments as $attachment_id => $attachment ) { // create the list items for images with captions

        echo '<li>';
        echo wp_get_attachment_image($attachment_id, '[string or another array for width and height]');
        echo '</li>';
    }

} // end see if images

wp_reset_postdata();

?>

_Cindy


Your second arg "p = 107" isn't valid.

What you could try is the get_attached_media() function.

And do something like this:

$media = get_attached_media('image', 2360);

foreach ($media as $m) {

    echo '<li>';
        echo wp_get_attachment_image( $m->ID, 'home-slider' );
    echo '</li>';                
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.