Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

jonytje on "Nested loop get_posts("

$
0
0

Hi,

I need to fix a nested loop: first of all i'm getting all posts and in the loop of posts i'm getting al attachments (images) twice but in a different order.

The problem is it only works on the first item and not on the second. How do i fixed this properly?

<div class="content">
    <?php
    $args = array('posts_per_page' => 5, 'offset' => 0);
    $projectCount = 0;
    $myposts = get_posts($args);
    foreach ($myposts as $post) : setup_postdata($post);
    $projectCount++;
        ?>
        <section class="project" id="<?php echo $projectCount ?>">
            <h2><?php the_title(); ?></h2><p><strong><?php echo wp_strip_all_tags(preg_replace('/<img[^>]+./', '', $post->post_content)); ?></strong></p>
            <div class="colHolder">
            <?php
            $media = get_posts(array(
                'post_parent' => $post->ID,
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'orderby' => 'title',
                'order' => 'ASC'
            ));
            //print_r($media);
            ?>
            <?php foreach($media as $key => $image): ?>
                <?php if(!empty($image->post_title)): ?>
                <div class="col<?php if(!empty($image->post_content)){echo $image->post_content;}else{echo 1;} ?> colImg">
                    <div class="img imgGrid" data-order="<?php echo $image->post_excerpt; ?>" style="background: url(<?php echo $image->guid ?>) 50% 50% no-repeat;">
                        <img src="<?php echo $image->guid ?>" alt="" />
                    </div>
                </div>
                <?php endif; ?>
            <?php endforeach; ?>

            <ul class="hiddenGal">
            <?php
                $allmedia = get_posts(array(
                    'post_parent' => $post->ID,
                    'post_type' => 'attachment',
                    'post_mime_type' => 'image',
                    'orderby' => 'caption',
                    'order' => 'ASC'
                ));
                //
                foreach($allmedia as $key => $imagel):
            ?>
                <li data-img="<?php echo $imagel->guid ?>" class="hiddenImage"></li>
            <?php endforeach; ?>
            </ul>
            </div>
        </section>

<?php
endforeach;
wp_reset_postdata();
?>

</div>

Viewing all articles
Browse latest Browse all 8245

Trending Articles