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.

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">
                                        <?php
                                        $category = get_the_category(); 
                                        echo $category[0]->cat_name;
                                        ?>
                                    </span>
                                </a>
                                </li>




                        <?php endwhile; ?>  

                        <?php echo '</ul>'; ?>
share|improve this question

1 Answer 1

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() ) :
        $featuredPosts->the_post();

    endwhile;

    echo '</ul>';

endif;
share|improve this answer

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.