0

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

1 Answer 1

2

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!

1
  • Perfect! Works great. Thanks for the guidance, learning more every day.
    – mcseay
    Commented May 17, 2013 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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