Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on a Wordpress website and I am using a loop to get all the posts that have title and body contain an image.

But the problem is that it returns me all the titles but not all the images. I don't understand why.

Here is the code:

<?php  query_posts('cat=17&order=ASC');
remove_filter('the_content', 'wpautop');

if (have_posts()) : while (have_posts()) : the_post();
    ?>
    <div class="photos">
        <?php $args = array('post_type' => 'attachment', 'numberposts' => 34, 'post_status' => null, 'post_parent' => $post->ID);
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ($attachments as $attachment) {
                ?>
                <a href="<?php echo $attachment->guid;?>" class="lb_gallery"><?php the_content();?></a>
            <?php
            }
        }?>
        <div class="fotos-desc">
            <span><?php the_title();?></span>
        </div>
    </div>

<?php endwhile; ?>
<?php endif; ?>

And here is the output on front-end: enter image description here

In the above image you can see titles like foto-1, foto-2, foto-3 and so on. All these titles are coming through loop but it displays the image for the first one only but all posts are having the image as in the foto-1.

share|improve this question

migrated from stackoverflow.com Jan 2 '13 at 16:24

This question came from our site for professional and enthusiast programmers.

    
You are probably missing to run setup_postdata($attachment); - Also some simple trouble-shooting like var_dump($attachments); will actually tell you what data is available and what data is missing. –  hakre Dec 28 '12 at 10:30
    
i try user suggestion but no luck –  Kashif Waheed Dec 28 '12 at 10:37
    
Yes, that was just a quick pointer. Take a look how the_content works so that you actually have it output the data you're looking for. Do the var_dump as well, there is more you can do to come to a solution, I can not run your code here. –  hakre Dec 28 '12 at 10:39
    
i think problem is in the array elements when i remove 'post_parent' => $post->ID it give me all but still not the correct one –  Kashif Waheed Dec 28 '12 at 10:46
1  
Then that does not look good, too. As written, you need to debug this. With guessing after five minutes it's burdensome. Find out what happens and why instead. –  hakre Dec 28 '12 at 10:51

2 Answers 2

You are trying to get the ID via $post->ID, but that will only return the post ID of the first post. You can use global $post; before the args array, or much simpler replace $post->ID by the function get_the_ID().

$args = array('post_type' => 'attachment', 'numberposts' => 34, 'post_status' => null, 'post_parent' => get_the_ID());
share|improve this answer

The problem here lies in the fact that you have a nested Loop which is not being terminated correctly. The inner get_posts() is upsetting your outer query_posts loop because you're not resetting this loop when you're done. Try this:

<?php  query_posts('cat=17&order=ASC');
remove_filter('the_content', 'wpautop');

if (have_posts()) : while (have_posts()) : the_post();
    ?>
    <div class="photos">
        <?php $args = array('post_type' => 'attachment', 'numberposts' => 34, 'post_status' => null, 'post_parent' => $post->ID);
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ($attachments as $attachment) {
                ?>
                <a href="<?php echo $attachment->guid;?>" class="lb_gallery"><?php the_content();?></a>
            <?php
            }

            // restore loop
            wp_reset_postdata();

        }?>
        <div class="fotos-desc">
            <span><?php the_title();?></span>
        </div>
    </div>

<?php endwhile; ?>
<?php endif; ?>

All I've done is added a wp_reset_postdata() when you're done with the inner loop to restore your outer one.

share|improve this answer
    
Do not use query_posts! There's exactly no reason to do this. –  kaiser Jan 15 '13 at 18:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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