• 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 &raquo;'); ?></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 &raquo;'); ?></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)
  • Start with this and see how you get on..
    https://wordpress.pastebin.com/P0D08gim

    The extra tabbing and spacing makes the code more readable and easier to follow..

    Untested, i’ve just reformatted your code a little and renamed some vars, etc..

    Hope that helps though.. ??

    Thread Starter fcarthy

    (@fcarthy)

    Hi Mark! Thank you! It’s working ALMOST perfectly. I really appreciate your time.

    The UL small-posts is now randomizing correctly without affecting the second post, which is what I needed. The only problem that’s happening now is that the small-posts UL is embeded within the overall “post” class, so the four small posts show up within the boundaries of the post. I tried to modify the code a little, all I did was move up the closing div for the post class above the UL, but I still get the same result. How can I make the UL show up between the posts independent of the post class?

    <?php get_header(); ?>
    
    <div id="content">
    
    	<?php
    	// First query
    	$my_query = new WP_Query;
    	$my_query->query( array( 'post_type' => 'clipping' ) );
    
    	// If first query have posts
    	if( $my_query->have_posts() ) :
    
    		$count = 0; 
    
    		// While first query have posts
    		while ($my_query->have_posts()) : $my_query->the_post(); 
    
    			// Setup vars
    			$count++;
    			$do_not_duplicate = $post->ID;
    			$clipping_image = get_post_meta( $post->ID, 'clipping_image', true );
    			$subtitle = get_post_meta( $post->ID, 'subtitle', true );
    			$incr_number = get_post_meta( $post->ID,'incr_number',true);
    			$terms_of_post = get_the_term_list( $post->ID, 'types', '','', '', '' );
    		?>
    
    		<div class="post" id="post-<?php the_ID(); ?>"><!-- start post -->
    
    			<div class="entry-content">
    				<?php if( $clipping_image ) : ?><a href="<?php the_permalink(); ?>"><img src="<?php echo $clipping_image ?>"></a><?php endif; ?>
    				<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    				<?php if( $subtitle ) echo $subtitle; ?>
    				<p><?php the_content('Read the rest of this entry &raquo;'); ?></p>
    				<p class="category"><?php if( $incr_number ) echo "#$incr_number"; ?> Posted in <?php if( $terms_of_post ) echo $terms_of_post; ?></p>
    			</div>
    		 </div>
    		 <!-- End post div -->
    
    			<?php
    			// If count is equal to 2
    			if( $count == 2 ) : 
    
    				// Second query
    				$my_second_query = new WP_Query;
    				$my_second_query->query( array( 'post_type' => 'clipping' , 'orderby' => 'rand' , 'posts_per_page' => '4' ) );
    
    				// If second query have posts
    				if( $my_second_query->have_posts() ) :
    				?>
    
    					<ul class="small-posts">
    
    					<?php
    					// While second query have posts
    					while( $my_second_query->have_posts() ) : $my_second_query->the_post();
    
    						// Setup vars
    						$do_not_duplicate = $post->ID;
    						$incr_number_two = get_post_meta( $post->ID, 'incr_number', true );
    						?>
    
    						<li>
    							<a href="<?php the_permalink() ?>" rel="bookmark"><?php image_attachment('clipping_image', 140, 140); ?></a>
    							<?php if( $incr_number_two ) echo '<p class="small-post-number">'. "#$incr_number_two" . '</p>'; ?>
    						</li>
    
    						<?php
    					// End second while have posts
    					endwhile;
    					?>
    
    					</ul>
    
    				<?php
    				// End if second query have posts
    				endif;
    
    			// End if count is equal to 2
    			endif;
    			?>
    
    		<?php
    		// End first while have posts
    		endwhile; 
    
    	// End if first query have posts
    	endif;
    	?>
    
    </div>
    <!-- end content -->
    <?php get_footer(); ?>

    Thanks again for your help!!

    That looks right, can you link to a page showing the problem?

    Thread Starter fcarthy

    (@fcarthy)

    I can’t see the second loop on that page, should i be looking somewhere in particular?

    Here’s an example from one of my sites, PromotedProfits that may be useful. I don’t think this is exactly what you’re looking for and it seems Mark has helped you, but you could use it in the future.

    On the homepage, in the sidebar here https://promotedprofits.com you see “featured marketers” and then another nested loop to show the most recent product review for that person (associated by tag).

    So the structure is:

    Featured Marketers
    ———————
    Person’s name (permalink)
    Excerpt
    Latest Person’s Name Review (heading)
    Product name (permalink)

    Here’s the code:

    https://wordpress.pastebin.com/MZh3pbqG

    Hope you get some use out of this…

    Cheers,
    Bryan

    Thread Starter fcarthy

    (@fcarthy)

    Hi Bryan, thanks for the tip.

    I’ve decided to remove the nested loop for now. The way I was envisioning it it creates a bit of confusion for the user. Great idea in concept, but not in execution.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Help with nested loops please!’ is closed to new replies.