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>'; ?>

1 Answer 1


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


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.