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 have a custom loop to show posts that meet a certain set of $args like this:

<?php $recent = new WP_Query( $orderargs );     
        while($recent->have_posts()) : $recent->the_post();?>

This works great when the $orderargs bring up posts, but in some situations, there will be no posts, and I want to show some default content then.

So what do I add to this / how do I restructure this so that if the query has no posts, display default content instead.

Thank you, Ian

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

You do that the same way like with regular posts:

if ( $recent->have_posts() )
{
    while($recent->have_posts())
    {
        $recent->the_post();
        // print posts
    }
}
else
{
    print 'no posts found';
}
share|improve this answer
    
Thanks toscho, That worked! –  IanB Oct 27 '12 at 14:04
add comment

Just you use the regular loop.

<?php $recent = new WP_Query( $orderargs ); while($recent->have_posts()) : $recent->the_post();?>

type your code here.

<?php endwhile; else: ?>

Put your default content here.

<?php endif; ?>
share|improve this answer
add comment

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.