WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My wp_query gets posts starting at the 5th one, proceeding it is another loop for the first four, for styling reasons.

How do I conditionally get the wrapper for these posts only if they exist?

The only way I can get it working is to have the container 'right-hang' before and after the loop, obviously if I put it inside the loop it the markup it spits out is just two ul's.

<?php echo('<ul class="right-hang">'); ?>

                                <?php $featuredPosts = new WP_Query(); ?>

                                <?php $featuredPosts->query('showposts=2&cat=6&offset=4'); ?>

                                <?php while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>

                                <li class="non-stacked">
                                <a href="<?php the_permalink() ?>">
                                    <?php the_post_thumbnail('extrathumb'); ?>
                                    <span><?php the_title(); ?></span>

                                    <span class="f-category">
                                        $category = get_the_category(); 
                                        echo $category[0]->cat_name;

                        <?php endwhile; ?>  

                        <?php echo '</ul>'; ?>
share|improve this question
up vote 0 down vote accepted

You can check if a query has posts with if( $your_query->have_posts() ), and only output the container if it passes that test.

$featuredPosts = new WP_Query( array(
    'posts_per_page' => 2,
    'cat' => 6,
    'offset' => 4
) );

if ( $featuredPosts->have_posts() ) :

    echo('<ul class="right-hang">');

    while ( $featuredPosts->have_posts() ) :


    echo '</ul>';

share|improve this answer

Your Answer


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.