• Resolved csleh

    (@csleh)


    There are 3 wp_query’s called, and each needs its own div. However, a div should only open and close if there are appropriate posts. I can get this to work if all have content with the end div in one spot, or it will work if only 2 have content with the /div in another spot. But I can’t get to work for both cases — if a page has all three and another has just 2.

    <div class="row">
    	<?php
    		$tags = wp_get_post_tags($post->ID);
    		if ($tags) {
    		  	$args=array(
      		  	  'caller_get_posts'=>1
     		 	 );
    		  $my_query = new WP_Query($args);
      		if( $my_query->have_posts() ) { ?>
    			<div class="columns four resource-list case">
     				<h4>title</h4>
    					<?php  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    						<?php the_excerpt(); ?>
       				        <?php  endwhile;
     		 } ?>  </div>
    	<?php wp_reset_query();  // Restore global post data stomped by the_post().
    	}
    	?>
    	<!-- repeat the above php 2 more times -->
    </div>

    I’ve tried after excerpt, after endwhile, and after php.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Well, the end /div should for sure be right after the endwhile; statement but before the closing if } so I don’t see how that would not have worked there.

    I am wondering why the h4 title would be before the while() loop starts?

    Thread Starter csleh

    (@csleh)

    Ah, the h4 title is the category heading. I typed it in to navigate the code easier. The args are edited here to keep it short.

    So it sounds like you think the /div is in the correct place here, right? What’s happening for me though is that if the second set doesn’t have any posts, the /div for that bit still prints, which messes up everything.

    I am truly stumped here.

    No, no. The end div should be like so:

    if( $my_query->have_posts() ) { ?>
    			<div class="columns four resource-list case">
     				<h4>title</h4>
    					<?php  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    						<?php the_excerpt(); ?>
       				        <?php  endwhile; ?>
    
     		 </div>
    <?php } ?>

    Remember, you are saying “if” there are any results do the following…otherwise skip the whole bit ??

    Thread Starter csleh

    (@csleh)

    OOOH I see it now. The bracket end was the key — I thought where I had the /div was the same thing. THank you for keeping hairs on my head.

    You value the hair on your head just like me!!! ha!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘trouble with closing div's in multiple wp_query loops’ is closed to new replies.