WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] trouble with closing div's in multiple wp_query loops (6 posts)

  1. csleh
    Member
    Posted 2 years ago #

    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.

  2. racer x
    Member
    Posted 2 years ago #

    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?

  3. csleh
    Member
    Posted 2 years ago #

    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.

  4. racer x
    Member
    Posted 2 years ago #

    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 ;)

  5. csleh
    Member
    Posted 1 year ago #

    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.

  6. racer x
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic