Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a left nav menu that contains a couple of different taxonomy lists of a custom post type. For example, the left menu might look like this...

<h2>New Stuff</h2>
<ul>
<li>Item 3</li>
<li>Item 4</li>
</ul>

<h2>Old Stuff</h2>
<ul>
<li>Item 2</li>
<li>Item 1</li>
</ul>

Below is the code I currently have in my sidebars template...

<h2>New Stuff</h2>
<ul>
<?php
    $args = array(
        'post_type' => 'stuff',
        'taxonomy' => 'stuff-type',
        'term' => 'new-stuff',
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' =>-1
    );
    query_posts($args);
        if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</ul>

<h2>Old Stuff</h2>
<ul>
<?php
    $args = array(
        'post_type' => 'stuff',
        'taxonomy' => 'stuff-type',
        'term' => 'old-stuff',
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' =>-1
    );
    query_posts($args);
        if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</ul>

This all works fine expect that I need the h2 and ul tags to not appear if there are no posts assigned to the taxonomy. How can I get the opening h2 title and ul wrappers somehow in the WordPress loop (just before and after), leaving the else/endif statement to print nothing (printing nothing if there are no posts for that taxonomy)?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

When you initiate a Loop, split it up like so:

<?php if (have_posts()) : ?>
           <?php while (have_posts()) : the_post(); ?>    
           <!-- do stuff ... -->
           <?php endwhile; ?>
 <?php endif; ?>

Anything inside the if statement but outside the while statement will run if have_posts is true, but will not be Looped for each Post.

Also, it's recommended that you use WP_Query, not query_posts - see here for more information. Be sure to reset the Loop after your query - it's recommended here that wp_reset_postdata is the best option when using WP_Query.

So, in conclusion you should be writing something like this:

<?php

// Your arguments    
$args = array(
    'post_type' => 'stuff',
    'taxonomy' => 'stuff-type',
    'term' => 'new-stuff',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' =>-1
);

// Let's get the query, using WP_Query
$loop = new WP_Query($args);

// Check if there are posts for our query
if ( $loop->have_posts() ) : ?> 

<h2>New Stuff</h2>
<ul>

    <?php 
    // Get looping    
    while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

    <?php endwhile; ?>

</ul>     

<?php endif; ?>
<?php wp_reset_postdata(); ?>

Hope that helps!

share|improve this answer
    
Perfect! Works great. Thanks for the guidance, learning more every day. –  mcseay May 17 '13 at 23:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.