0

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>

2 Answers 2

0

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>';                
}
2
  • thank you! I will try this and get back w you. Getting the id of a post is so confusing when you're trying to learn the wp_query reference page in the codex... so what is the valid way to call the id of a post in the conditions/arguments of the wp_query? Is it ` 'p' => '107' ` ? If so, it didn't work in this particular query.
    – ccbar
    Mar 17, 2015 at 14:09
  • nothing is working, wp_query, get_attached_media - nothing.
    – ccbar
    Mar 17, 2015 at 20:05
0

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 Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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