• Hi,

    I have a problem that I haven’t been able to wrap my head around yet. In a nutshell, I have a loop with a div wrapper (.partnersBlock) and title (Researchers) that come before the loop itself. What I need to do is set it up so that the div and title don’t appear until a post has been added to that particular loop. This would be set up with conditionals, I just can’t figure out how exactly to write it. Here is my current code:

    <?php if (have_posts()) : ?>
    
    <?php query_posts('category_name=researchers&showposts=50&order=ASC'); ?>
    
            <div class="partnersBlock"><!--this should only appear if there are posts below-->
            <h2>Researchers</h2><!--this should only appear if there are posts below--> 
    
    <?php while (have_posts()) : the_post(); ?>
    	<div class="largeProfile">
    	<div class="post" id="post-<?php the_ID(); ?>">
    	<h3><?php the_title(); ?></h3>
    	<?php the_content(); ?>
    	</div>
    	</div>
    <?php endwhile; else: ?>
    
    	<p>Sorry, no posts matched your criteria.</p>
    
            </div><!--this should only appear if there are posts above--> 
    
    <?php endif; ?>

    I’m building the functionality so the user can add posts when they’re ready, but until that point no code is activated. As it stands this is printed:

    <div class="partnersBlock">
    <h2>Researchers</h2>
    </div>

    Any help is appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try this

    <?php $div_printed = false; ?>
    
    <?php if (have_posts()) : ?>
    	<?php query_posts('category_name=researchers&showposts=50&order=ASC'); ?>
    	<?php while (have_posts()) : the_post();
    		if (!$div_printed) { ?>
                  <div class="partnersBlock">
    	      <h2>Researchers</h2>
    	      <?php $div_printed = true;
    		} ?>
    		<div class="largeProfile">
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<h3><?php the_title(); ?></h3>
    				<?php the_content(); ?>
    			</div>
    		</div>
    	<?php endwhile; else: ?>
    		<p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    <?php if ($div_printed) { ?>
              </div>
    <?php } ?>

    Shouldn’t query_posts come before if have posts?

    Maybe?…

    <?php query_posts('category_name=researchers&showposts=50&order=ASC'); ?>
    
    <?php if (have_posts()) : ?>
    
    	<div class="partnersBlock">
    		<h2>Researchers</h2>
    
    		<?php while (have_posts()) : the_post(); ?>
    			<div class="largeProfile">
    				<div class="post" id="post-<?php the_ID(); ?>">
    					<h3><?php the_title(); ?></h3>
    					<?php the_content(); ?>
    				</div>
    			</div>
    		<?php endwhile; ?>
    
    	</div>
    
    <?php else: ?>
    
    	<p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>

    Oh, yes, that is correct about query posts before have posts. I forgot to move that in my code.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display content based on whether there are posts in loop’ is closed to new replies.