Help with nested loops please!
-
I have a loop of posts running on my index page. I was doing some research on how to embed advertisement only between the first and second post on the page, and then it got me thinking. What if I tried to embed another wordpress loop between post 1 and two? Basically I want to have one big loop, and in between post 1 and 2 have a second loop that calls four posts, of the same post type, randomly. This is what I currently have:
<?php get_header(); ?> <div id="content"> <?php $my_query = new WP_Query( array( 'post_type' => 'clipping' , 'showposts' => '4' ) ); ?> <?php $count = 0; ?> <?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate=$post->ID;?> <?php $count++; ?> <?php if ($count == 2) : ?> <?php $my_query = new WP_Query( array( 'post_type' => 'clipping' , 'orderby' => 'rand' , 'showposts' => '4' ) ); ?> <ul class="small-posts"> <?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate=$post->ID;?> <li> <a href="<?php the_permalink() ?>" rel="bookmark"><?php image_attachment('clipping_image', 140, 140); ?></a> <p class="small-post-number">#<?php echo get_post_meta($post->ID,'incr_number',true); ?></p> </li> <?php endwhile; ?> </ul> <div class="post" id="post-<?php the_ID(); ?>"><!-- start post --> <div class="entry-content"> <a href="<?php the_permalink(); ?>"><?php $photo_url="clipping_image";?><img src="<?php echo get_post_meta($post->ID, $photo_url, true); ?>"></a> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php $subtitle="subtitle";?><?php echo get_post_meta($post->ID, $subtitle, true); ?> <p><?php the_content('Read the rest of this entry »'); ?></p> <p class="category">#<?php echo get_post_meta($post->ID,'incr_number',true); ?> posted in <?php $terms_of_post = get_the_term_list( $post->ID, 'types', '','', '', '' ); echo $terms_of_post; ?></p> </div> <?php else : ?> <div class="post" id="post-<?php the_ID(); ?>"><!-- start post --> <div class="entry-content"> <a href="<?php the_permalink(); ?>"><?php $photo_url="clipping_image";?><img src="<?php echo get_post_meta($post->ID, $photo_url, true); ?>"></a> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php $subtitle="subtitle";?><?php echo get_post_meta($post->ID, $subtitle, true); ?> <p><?php the_content('Read the rest of this entry »'); ?></p> <p class="category">#<?php echo get_post_meta($post->ID,'incr_number',true); ?> posted in <?php $terms_of_post = get_the_term_list( $post->ID, 'types', '','', '', '' ); echo $terms_of_post; ?></p> </div> <?php endif; ?> </div><!-- end the post --> <?php endwhile; ?> </div><!-- end content --> <?php get_footer(); ?>
This is my whole index.php, sorry for the mess. I hope someone out there has tried something like this! Thanks in advance.
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Help with nested loops please!’ is closed to new replies.